Implementation of the idea is actually very simple, is a custom linearlayout, and TextView can loop vertical scrolling, and entries can be clicked, display area display up to 2 entries, and the alternating properties of the vertical movement of animation effect, through the thread to control the implementation of the scroll.
Not much to say see effect:
Code implementation
Let's start by setting custom properties for the control:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Jdadverview" >
<attr name= "gap" format= "integer"/>
<attr name= "animduration" format= "integer"/>
< /declare-styleable>
</resources>
The custom control's Get property methods are the same:
Gets the custom attribute
TypedArray array = context.obtainstyledattributes (Attrs, r.styleable.jdadverview);
Madverheight = Typedvalue.applydimension (Typedvalue.complex_unit_dip, Jdadverheight, GetResources (). Getdisplaymetrics ());
int gap = Array.getinteger (R.styleable.jdadverview_gap, mgap);
int animduration = Array.getinteger (r.styleable.jdadverview_animduration, manimduration);
Turn off emptying typedarray to prevent memory leaks
array.recycle ();
And then, let's take a look at the layout of the entries:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" 60DP "
android:background=" #ffffff "
android:gravity=" center_vertical "
android:orientation=" Horizontal ">
<textview
Android:id= "@+id/tag"
android:textcolor= "#ff0000"
android:layout_marginleft= "10DP"
android:text= "Latest"
android:background= "@drawable/corner"
android:textsize= "18sp"
android:padding= "5DP"
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"/>
<textview
Android:id= "@+id/title"
android:layout_marginleft= "10DP"
android:singleline= "true"
Android: Ellipsize= "End"
android:textsize= "20sp"
android:text= "Price stunned! Telecom Gigabit Optical Fiber Listing "
android:textcolor=" #000000 "
android:layout_width=" wrap_content "
android:layout_height = "Wrap_content"/>
</LinearLayout>
The layout is simple and the effect is:
Do not explain, we have to write the adapter:
Package com.example.jdadvernotice;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Com.example.jdadvernotice.entity.AdverNotice;
Import Com.example.jdadvernotice.view.JDAdverView;
Import java.util.List;
/** * Created by the Administrator on 2016/3/20. * Jingdong Advertisement Data Adapter * */public class Jdviewadapter {private list<advernotice> Mdatas, public jdviewadapter (List<advern Otice> mdatas) {this.mdatas = Mdatas; if (Mdatas = null | | mdatas.isempty ()) {throw new RuntimeException ("Nothing to
Show ");
/** * Get Data number of bars * @return/public int getcount () {return mdatas = null? 0:mdatas.size ();}
/** * Get a touch of data * @param position * @return/public advernotice getitem (int position) {return mdatas.get (position);}/** * Get the entry layout * @param parent * @return/Public View GetView (jdadverview parent) {return Layoutinflater.from Parent.getconte
XT ()). Inflate (R.layout.item, NULL); /** * Entry Data fit * @param view * @paRAM Data */public void SetItem (final view view, final Advernotice data) {TextView TV = (TextView) View.findviewbyid (r.id
. title);
Tv.settext (Data.title);
TextView tag = (TextView) View.findviewbyid (R.id.tag);
Tag.settext (Data.url); You can add click event View.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view v) {//= open URL Toast.
Maketext (View.getcontext (), Data.url, Toast.length_short). Show ();
}
}); }
}
And then we'll come from the definition view:
Package Com.example.jdadvernotice.view;
Import Android.animation.Animator;
Import Android.animation.AnimatorListenerAdapter;
Import Android.animation.AnimatorSet;
Import Android.animation.ObjectAnimator;
Import Android.content.Context;
Import android.content.res.Configuration;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;
Import Android.view.View;
Import Android.widget.LinearLayout;
Import Com.example.jdadvernotice.JDViewAdapter;
Import COM.EXAMPLE.JDADVERNOTICE.R;
/** * Created by Zengyu on 2016/3/20. * * public class Jdadverview extends LinearLayout {//control height private float madverheight = 0f;//time interval private final int mgap =
4000;
Animation time interval private final int manimduration = 1000;
Display the dimensions of text private final float textsize = 20f;
Private Jdviewadapter Madapter;
Private final float jdadverheight = 50;
The view private view Mfirstview displayed; Private View Msecondview;
Playing subscript private int mposition;
Identity of the thread private Boolean isstarted;
Brush private Paint Mpaint;
Public Jdadverview {This (context, null);}
Public Jdadverview (context, AttributeSet attrs) {This (context, attrs, 0);} Public Jdadverview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Init (con
Text, Attrs, defstyleattr); /** * Initialization Properties * @param context * @param attrs * @param defstyleattr * * * private void init (context, AttributeSet at
TRS, int defstyleattr) {//set to Vertical direction setorientation (VERTICAL);//antialiasing effect mpaint = new Paint (paint.anti_alias_flag);//Get Custom properties
TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.jdadverview); Madverheight = Typedvalue.applydimension (Typedvalue.complex_unit_dip, Jdadverheight, GetResources ().
Getdisplaymetrics ());
int gap = Array.getinteger (R.styleable.jdadverview_gap, mgap); int animduration = Array.getinteger (r.styleable.jdadverview_animduration, MAniMduration);
if (mgap <= manimduration) {gap = Mgap; animduration = manimduration;}//Close empty Typedarray array.recycle ();
/** * Set data/public void Setadapter (Jdviewadapter adapter) {This.madapter = adapter; Setupadapter ();} /** * Open thread/public void start () {if (!isstarted && madapter.getcount () > 1) {isstarted = true; postdelayed (Mrunnable, mgap);//Interval Mgap refresh UI}}/** * pause scrolling/public void Stop () {//Remove handle update removecallbacks (mrunnable);//Suspend thread I
sstarted = false;
/** * Set data adapter/private void Setupadapter () {//Remove all View removeallviews ()///Only one data, do not roll East if (madapter.getcount () = = 1) {
Mfirstview = Madapter.getview (this);
Madapter.setitem (Mfirstview, Madapter.getitem (0));
AddView (Mfirstview); else {//multiple data Mfirstview = Madapter.getview (this); Msecondview = Madapter.getview (this); Madapter.setitem (Mfirstview, M
Adapter.getitem (0));
Madapter.setitem (Msecondview, Madapter.getitem (1));
Add 2 to this control AddView (Mfirstview);
AddView (Msecondview);
Mposition = 1; IsStarted= false; /** * Measuring control's width high * * @param widthmeasurespec * @param heightmeasurespec/@Override protected void onmeasure (int widthme Asurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, Heightmeasurespec); if (LAYOUTPARAMS.WRAP_
CONTENT = = Getlayoutparams (). Height) {getlayoutparams (). Height = (int) madverheight;} else {madverheight = GetHeight (); } if (Mfirstview!= null) {Mfirstview.getlayoutparams (). Height = (int) madverheight;} if (Msecondview!= null) {Mseco
Ndview.getlayoutparams (). Height = (int) madverheight; }/** * Draw layout * * @param canvas * * * @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); Mpaint.setcolor
(Color.White); Mpaint.settextsize (Typedvalue.applydimension (typedvalue.complex_unit_sp, TEXTSIZE, GetResources ().
Getdisplaymetrics ()));
Mpaint.setstyle (Paint.Style.STROKE); Canvas.drawtext ("Swiss Vader Saber", Textsize, GetHeight () * 2/3, mpaint);//write Text 2/3 height}/** * Vertical fuck/private void Performswitch () {//Property animation Control control scrolling, y-axis move Objectanimator AnimaTor1 = Objectanimator.offloat (Mfirstview, "Translationy", Mfirstview.gettranslationy ()-madverheight); Objectanimator Animator2 = objectanimator.offloat (Msecondview, "Translationy", Msecondview.gettranslationy ()-
Madverheight);
Animation Set Animatorset set = new Animatorset (); Set.playtogether (Animator1, Animator2);//2 animations together Set.addlistener (new Animatorlisteneradapter () {@Override public void
Onanimationend (animator Animation) {//animation end Mfirstview.settranslationy (0); msecondview.settranslationy (0);
View Removedview = getchildat (0);//Get the first child layout mposition++;
Set the layout shown Madapter.setitem (Removedview, Madapter.getitem (mposition% Madapter.getcount ()));
Remove the previous view Removeview (Removedview);
Add next View AddView (Removedview, 1);
}
});
Set.setduration (manimduration);//Duration Set.start ();//Open animation} private animrunnable mrunnable = new animrunnable ();
Private class Animrunnable implements Runnable {@Override public void run () {performswitch (); postdelayed (this, mgap); /** * Destroy view when calling/@Override ProteCTED void Ondetachedfromwindow () {Super.ondetachedfromwindow ();/stop scrolling Stop ();/** * Screen rotation * * @param newconfig/@Ove Rride protected void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig);}}
As you can see from the above, the control can display up to 2 entries, and with thread control, rotate the display according to the subscript of the entry.
Use code specifically:
Initializing data:
private void InitData () {
datas.add (new Advernotice ("Swiss Vader saber New 200-50", "latest"));
Datas.add (New Advernotice ("Home refresh season, say 199 minus 100!") "," most popular "));
Datas.add (New Advernotice ("Take the camera to spring outing, Nikon low to 477", "hot"));
Datas.add (New Advernotice) ("Price stunned!") Telecommunications Gigabit Optical Fiber Listing "," new ")");
}
The binding adapter opens a scrolling thread:
InitData ();
Final Jdviewadapter adapter = new Jdviewadapter (datas);
Final Jdadverview Tbview = (jdadverview) Findviewbyid (r.id.jdadver);
Tbview.setadapter (adapter);
Open Thread Roller East
tbview.start ();
Write here, very late to sleep, welcome everyone to come to shoot bricks.
The above content is for Android high imitation Jingdong vertical circular rolling news bar of all introduction, hope to help everyone above!