Android News Banner Scrolling effect _android

Source: Internet
Author: User
Tags time interval

The project needs to use a similar bulletin board control, can use the basic does not support multi-line display, so had to do their own, suffering from a decent control has not been customized, for reference to the Android bulletin Board demo, to achieve a multiline scroll up control. Add the following functionality on top of the original control:
• Incoming data pagination display
• Add left Drawable
• Finger Touch Event Handling
• Add 3D animation Rollover effect

Effect chart

Source

Package Com.android.view;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Camera;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import android.graphics.drawable.Drawable;
Import Android.os.Handler;
Import Android.os.Looper;
Import Android.text.TextPaint;
Import Android.text.TextUtils;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.animation.AccelerateInterpolator;
Import android.view.animation.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationSet;
Import android.view.animation.Transformation;
Import android.view.animation.TranslateAnimation;
Import Android.widget.FrameLayout;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;

Import COM.SD2W.MARKET.CLIENT.R;
Import java.util.ArrayList; ImporT java.util.List;
Import static Android.view.ViewGroup.LayoutParams.MATCH_PARENT;

Import static Android.view.ViewGroup.LayoutParams.WRAP_CONTENT; /** * Bulletin Rolling area * * @author Qilian @version 1.0 * @date 2016-08-17/public class Rollingview extends Framelayout

 ments Onclicklistener {//Default animation execution time private static final int animation_duration = 1000;
 Deferred scrolling time interval private long mduration = 3000;
 Font color private int mtextcolor = 0xff000000;
 After clicking the font color private int mclickcolor = 0XFF0099FF;
 Font size private float mtextsize = 14;
 Line spacing private int mtextpadding = 10;
 Brush private Paint Mpaint;
 Default per page of information private int mpagesize = 3;
 Last page remainder private int muplimited = Mpagesize;
 Current display page number private int mcurrentpage = 0;
 Total number of pages private int mpagecount;
 Left picture private int mleftdrawable;
 Paging data Object private list<linearlayout> mrollingpages;
 Default animation private Animationset Menteranimset;
 Private Animationset Mexitanimset;
 Private Rollingrunnable mrunnable; Private Handler Mhandler;
 Private Onitemclicklistener Mclicklistener;
 Layout parameters private Layoutparams mframeparams;
 Private Linearlayout.layoutparams Mlinearparams;
 Menterdownanim,moutup respectively constitute a downward page of the animation private rotate3danimation Menterdownanim;

 Private Rotate3danimation Mexitupanim;
 Menterupanim,moutdown respectively constitute a downward page of the animation private rotate3danimation Menterupanim;

 Private Rotate3danimation Mexitdownanim;
 Public Rollingview {This (context, NULL);
  Public Rollingview (context, AttributeSet attrs) {Super (context, attrs);
  Gets properties from XML TypedArray array = GetContext (). Obtainstyledattributes (Attrs, R.styleable.rollingview);
  Mtextsize = Array.getdimension (r.styleable.rollingview_textsize, mtextsize);
  Mtextcolor = Array.getcolor (R.styleable.rollingview_textcolor, Mtextcolor);
  Array.recycle ();
  Create default display hidden animation createenteranimation ();
  Createexitanimation ();
  Initializes the brush mpaint = new Textpaint (); Initializes the Handler object mhandler = new Handler (Looper.getmainloOper ());
  Menterdownanim = Createanim ( -90, 0, True, true);
  Mexitupanim = Createanim (0, N, False, True);
  Menterupanim = Createanim (0, True, false);
 Mexitdownanim = Createanim (0, -90, false, false); Private rotate3danimation Createanim (float start, float end, Boolean turnin, Boolean turnup) {final Rotate3danimati
  On rotation = new Rotate3danimation (start, End, Turnin, turnup);
  Rotation.setduration (300);
  Rotation.setfillafter (FALSE);
  Rotation.setinterpolator (New Accelerateinterpolator ());
 return rotation; /** * Set Paging size * * @param pageSize */public void setpagesize (int pageSize) {this.mpagesize = This.muplimite
 D = pageSize; /** * Set DELAY time * * @param millionseconds/public void setdelayedduration (long millionseconds) {This.mdurat
 ion = Millionseconds; /** * Settings Display animation * * @param animation */public void setenteranimation (Animationset animation) {Menteranimset
 = animation;
 /** * Set Hidden animation * * @param animation */public void setexitanimation (Animationset animation) {mexitanimset = animation;
 /** * Set Line spacing * * @param padding/public void settextpadding (int padding) {this.mtextpadding = padding;
 /** * Set Click after font color * * @param color/public void setclickcolor (int color) {this.mclickcolor = color; /** * Set Left picture * * @param drawable */public void setleftdrawable (int drawable) {this.mleftdrawable = Drawabl
 E /** * Set Click event * * @param clicklistener */public void Setonitemclicklistener (Onitemclicklistener clicklistener
  {if (null = = Clicklistener) return;
 This.mclicklistener = Clicklistener;  @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec,
  HEIGHTMEASURESPEC);
  If the size is not specified, then set width to 300px int exceptwidth = 300;
  int exceptheight = 0;
  The height is calculated, and if you set the height to textsize it will be ugly because the text has the default upper and lower margins. if (Measurespec.getmode (heightmeasurespec)!= measurespec.exactly) {if (mtextSize > 0) {mpaint.settextsize (mtextsize);
    Paint.fontmetrics FontMetrics = Mpaint.getfontmetrics ();
   Exceptheight = (int) (fontmetrics.bottom-fontmetrics.top);
  int width = resolvesize (exceptwidth, Widthmeasurespec);
  int height = resolvesize (exceptheight, Heightmeasurespec);
 Setmeasureddimension (width, height);
  } public void Setrollingtext (list<string> array) {if (null = = Array | | array.isempty ()) return;
  This.removeallviews ();
  if (mrollingpages = = null) {mrollingpages = new arraylist<> ();
  } mrollingpages.clear ();
  Compute quotient int quotient = Array.size ()/mpagesize;
  Calculates the remainder int remainder = array.size ()% Mpagesize; Calculate how many pages to create Mpagecount = remainder = = 0?
  Quotient:quotient + 1;
   for (int i = 0; i < Mpagecount i++) {//Create a layout linearlayout container = CreateContainer ();
   if (i = = mPageCount-1) {muplimited = remainder = = 0? mpagesize:remainder;
for (int n = 0; n < muplimited; n++) {    TextView TextView = Createtextview (Array.get (mpagesize * i + N));
   Container.addview (TextView);
   }//Add to pagination mrollingpages.add (container);
  This.addview (container);
  }//Initialize display first page mcurrentpage = 0;
  Mrollingpages.get (mcurrentpage). setvisibility (VISIBLE);
 This.setvisibility (Mrollingpages.get (mcurrentpage)); /** * Create Page Object * * @return/private LinearLayout CreateContainer () {if (mframeparams = = null) {Mframepa
   Rams = new Layoutparams (match_parent, wrap_content);
  mframeparams.gravity = gravity.center_vertical;
  } linearlayout container = new LinearLayout (GetContext ());
  Container.setlayoutparams (Mframeparams);
  Container.setorientation (linearlayout.vertical);
 return container;
  private void Setvisibility (LinearLayout container) {int count = Container.getchildcount ();
  for (int i = 0; i < count; i++) {Container.getchildat (i). setvisibility (VISIBLE); /** * Create Page Content Object * * @param text * @return/private TexTview Createtextview (String text) {if (Mlinearparams = null) {mlinearparams = new Linearlayout.layoutparams (wrap_c
   Ontent, wrap_content);
  mlinearparams.gravity = gravity.center_vertical;
  } TextView TextView = new TextView (GetContext ());
  Textview.setlayoutparams (Mlinearparams);
  Textview.setsingleline ();
  Textview.setpadding (mtextpadding, mtextpadding, mtextpadding, mtextpadding);
  Textview.setellipsize (TextUtils.TruncateAt.END);
  Textview.settextcolor (Mtextcolor);
  Textview.setvisibility (invisible);
  Textview.settext (text);
   if (mleftdrawable > 0) {drawable drawable = GetContext (). Getresources (). getdrawable (mleftdrawable);
   Bitmap Bitmap = Bitmapfactory.decoderesource (GetContext (). Getresources (), mleftdrawable);
   drawable drawable = new Bitmapdrawable (GetContext (). Getresources (), bitmap);
   Drawable.setbounds (0, 0, 10, 10);
   Textview.setcompounddrawablepadding (10);
  Textview.setcompounddrawables (drawable, NULL, NULL, NULL); } textview.seTonclicklistener (this);
  Sets the font size if (Mtextsize > 0) {textview.settextsize (typedvalue.complex_unit_px, mtextsize);
 return textView;
  private void Createenteranimation () {menteranimset = new Animationset (false); Translateanimation translateanimation = new Translateanimation (0, 0, 0, 0, translateanimation.relative_to_parent, 1f,
  Translateanimation.relative_to_self, 0f);
  Alphaanimation alphaanimation = new Alphaanimation (0f, 1f);
  Menteranimset.addanimation (translateanimation);
  Menteranimset.addanimation (alphaanimation);
 Menteranimset.setduration (animation_duration);
  private void Createexitanimation () {mexitanimset = new Animationset (false); Translateanimation translateanimation = new Translateanimation (0, 0, 0, 0, translateanimation.relative_to_self, 0f,
  Translateanimation.relative_to_parent, -1f);
  Alphaanimation alphaanimation = new Alphaanimation (1f, 0f);
  Mexitanimset.addanimation (translateanimation); Mexitanimset.addanimation (Alphaanimation);
 Mexitanimset.setduration (animation_duration);
  @Override public boolean ontouchevent (Motionevent event) {int action = event.getaction ();
    Switch (action) {case MotionEvent.ACTION_DOWN:pause ();
   Break
    Case MotionEvent.ACTION_MOVE:case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:resume ();
  Break
 return true;
  The public void Resume () {///Only one page does not switch if (Mpagecount < 1) return;
  if (mrunnable = = null) {mrunnable = new rollingrunnable ();
  else {mhandler.removecallbacks (mrunnable);
 } mhandler.postdelayed (Mrunnable, mduration);
  public void Pause () {if (mrunnable!= null) {mhandler.removecallbacks (mrunnable);
  @Override public void OnClick (View v) {if (null = Mclicklistener) return;
  TextView TextView = (TextView) v;
  Mclicklistener.onitemclick (TextView);
 Textview.settextcolor (Mclickcolor);

  /** * Hide Current page, display next page Task/public class Rollingrunnable implements Runnable {@Override public void Run () {//Hide current page LinearLayout CurrentView = Mrollingpages.get (mcurrentpage);
   Currentview.setvisibility (invisible);
   if (Mexitanimset!= null) {currentview.startanimation (mexitanimset);//Mexitupanim);
   } mcurrentpage++;
   if (mcurrentpage >= mpagecount) {mcurrentpage = 0;
   //Show Next page linearlayout NextView = Mrollingpages.get (mcurrentpage);
   Nextview.setvisibility (VISIBLE);
   Setvisibility (NextView);
   if (Menteranimset!= null) {nextview.startanimation (menteranimset);//Menterdownanim);
  } mhandler.postdelayed (this, mduration);
  } public class Rotate3danimation extends Animation {private final float mfromdegrees;
  Private final float mtodegrees;
  Private Final Boolean mturnin;
  Private Final Boolean mturnup;
  private float Mcenterx;
  private float mcentery;

  Private Camera Mcamera; Public rotate3danimation (float fromdegrees, float todegrees, Boolean turnin, Boolean turnup) {mfromdegrees =Fromdegrees;
   Mtodegrees = todegrees;
   Mturnin = Turnin;
  Mturnup = Turnup; @Override public void Initialize (int width, int height, int parentwidth, int parentheight) {super.initialize (WI
   DTH, height, parentwidth, parentheight);
   Mcamera = new Camera ();
   Mcentery = GetHeight ()/2;
  Mcenterx = GetWidth ()/2;  @Override protected void applytransformation (float interpolatedtime, transformation t) {final float fromdegrees
   = Mfromdegrees;

   Float degrees = fromdegrees + ((mtodegrees-fromdegrees) * interpolatedtime);
   Final float CenterX = Mcenterx;
   Final float centery = mcentery;
   Final Camera Camera = Mcamera; Final int derection = Mturnup?

   1:-1;

   Final Matrix matrix = T.getmatrix ();
   Camera.save ();
   if (mturnin) {camera.translate (0.0f, Derection * mcentery * (interpolatedtime-1.0f), 0.0f);
   else {camera.translate (0.0f, Derection * mcentery * (interpolatedtime), 0.0f);
   } camera.rotatex (degrees); Camera.Getmatrix (matrix);

   Camera.restore ();
   Matrix.pretranslate (-centerx,-centery);
  Matrix.posttranslate (CenterX, centery);
 } public interface Onitemclicklistener {void Onitemclick (TextView v);
 }
}

Use

Initialize the list of extras
list<string> Haowaiarray = new arraylist<> ();
Haowaiarray.add ("[Mother and Child world] buy urine not wet send baby hand wet towel");
Haowaiarray.add ("[min Li shop] full 100 free distribution");
Haowaiarray.add ("[Fruit House] Thailand Golden Pillow Durian 8 Yuan/kg");
Haowaiarray.add ("[Outdoor sports] Outdoor Sports professional equipment Collection");
Haowaiarray.add ("[Daily specials] as long as 9.9 also package mail");
Haowaiarray.add ("[cutting-edge tidal products] folding electric vehicles");
Haowaiarray.add ("[Black tech] Intelligent VR takes you");
Haowaiarray.add ("[Travel essential] solar charge treasure-never power off");
Binding Data
Mrollingview.setpagesize (4);
Mrollingview.setclickcolor (0xff888888);
Mrollingview.setleftdrawable (R.DRAWABLE.DRAWABLE_RED_DOT);
Mrollingview.setrollingtext (Haowaiarray);
Mrollingview.setonitemclicklistener (this);

...

@Override public
void Onitemclick (TextView v) {
 //handle Item click event
}

@Override
public void Onresume () {
 super.onresume ();
 Mrollingview.resume ();
}
@Override public
void OnPause () {
 super.onpause ();
 Mrollingview.pause ();
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.