Some time ago the product let me do an animation, the requirement is the horizontal line of the way, in portrait display pictures, finally show a few seconds of animation in the original way back, and then I find information on the Internet, found that there are few records, and finally wrote a
Later, it will gradually improve;
Reprint please indicate source: http://www.cnblogs.com/hyylog/p/5659619.html
Step: 1, first use the control of the OnDraw () method, combined with the rise of the number of animation, to achieve the steps of the dash;
2. Using the Postinvalidate () method, call the Dispatchdraw () method to show the picture
The specific code is as follows:
public class Expandview extends LinearLayout {
Private Bitmap Mbitmap;
Private int linearduration = 500;
Private int expandduration = 800;
Private int dismissduration = 1500;
Private Paint Mppaint;
Private Paint Bitpaint;
Private int resultlinear = 40;//Dash animation distance to finish starting the next animation (to prevent the lag of animation start delay)
Private int resultbitmap = 30;//Paint animation distance Start The distance of the secondary animation (to prevent the lag of the animation start delay)
Private int type = 1;//1 to expand Animation 2 for retraction animation
Private Valueanimator valueanimator = null;
Private Valueanimator ValueAnimator2 = null;
Private int radus = 8;
Private int velua = 0;
Private int velualinear = 0;
Private View view;
Private Boolean Isstart = false;//Ensure animation starts only once
private Handler Handler = new Handler () {
public void Handlemessag E (android.os.Message msg) {
Switch (msg.what) {
Case 0:
Velua = 0;
Invalidate ();
break;
Case 1:
Start (2);
Break
Default:
break;
}
};
};
Public Expandview (context context, AttributeSet Attrs) {
Super (context, attrs);
Inite ();
}
public void Setview (view view) {
This.view = view;
}
Public Expandview (Context context) {
Super (context);
Inite ();
}
public void Inite () {
Bitpaint = new Paint (Paint.anti_alias_flag);
Mppaint = new Paint (Paint.anti_alias_flag);
Mppaint.setstrokewidth (5);
Mppaint.setcolor (Color.Black);
}
public void Setradus (int radus) {
This.radus = Radus;
}
public void setdistance (int linear, int bit) {
Resultbitmap = bit;
Resultlinear = linear;
}
public void setduration (int linearduration, int expandduration,
int dismissduration) {
This.linearduration = linearduration;
This.expandduration = expandduration;
This.dismissduration = dismissduration;
}
public void Setlinearpaintcolor (int color) {
Mppaint.setcolor (color);
}
public void start (int type) {
This.type = type;
Valueanimator.cancel ();
Valueanimator2.cancel ();
Handler.removemessages (1);
Handler.sendemptymessage (0);
Isstart = true;
if (type = = 1) {
Valueanimator.start ();
} else if (type = = 2) {
Valueanimator2.start ();
}
}
public void Initedanimation () {
Clearanimations ();
Mbitmap = Getbitmap ();
LOG.I ("", "pppp====velualinear=>>" + (Mbitmap.getwidth ()-Radus * 2));
Valueanimator = Valueanimator.ofint (0, Mbitmap.getwidth ()-Radus * 2);
ValueAnimator2 = Valueanimator.ofint (0, Mbitmap.getheight ());
Valueanimator.setduration (linearduration);
Valueanimator2.setduration (expandduration);
Valueanimator.addupdatelistener (New Animatorupdatelistener () {
@Override
public void Onanimationupdate (Valueanimator animation) {
Velualinear = (Integer) animation.getanimatedvalue ();
if (Velualinear > (Mbitmap.getwidth ()-resultlinear) && type = = 1) {
if (Isstart) {
Valueanimator2.start ();
Isstart = false;
}
}
Postinvalidate ();
}
});
Valueanimator2.addupdatelistener (New Animatorupdatelistener () {
@Override
public void Onanimationupdate (Valueanimator animation) {
Velua = (Integer) animation.getanimatedvalue ();
Postinvalidate ();
velualinear = 0;
}
});
}
@Override
protected void Dispatchdraw (canvas canvas) {
Super.dispatchdraw (canvas);
if (mbitmap! = null) {
Rect src;
Rect des;
if (type = = 1) {
Canvas.drawline (mbitmap.getwidth ()-Radus, 0,mbitmap.getwidth ()-Radus-velualinear, 0,mppaint) ;
src = new Rect (0, 0, mbitmap.getwidth (), Velua);
des = new Rect (0, 0, mbitmap.getwidth (), Velua);
} else {
Canvas.drawline (mbitmap.getwidth ()-Radus, 0, radus+ velualinear, 0, mppaint);
src = new Rect (0, 0, mbitmap.getwidth (), Mbitmap.getheight ()-Velua);
des = new Rect (0, 0, mbitmap.getwidth (), Mbitmap.getheight ()-Velua);
}
Canvas.drawbitmap (Mbitmap, src, des, bitpaint);
if (Velua = = Mbitmap.getheight () && type = = 1) {
Handler.removemessages (1);
Handler.sendemptymessagedelayed (1, dismissduration);
}
if (type = = 2 && velua >= mbitmap.getheight ()-Resultbitmap) {
if (isstart) {
Valueanimator.sta RT ();
Isstart = false;
}
}
}
}
public void Clearanimations () {
if (valueanimator! = null)
Valueanimator.cancel ();
if (ValueAnimator2! = null)
Valueanimator.cancel ();
Handler.removemessages (1);
velualinear=0;
Handler.sendemptymessage (0);
if (Mbitmap! = null &&!mbitmap.isrecycled ()) {
Mbitmap.recycle ();
}
}
Private Bitmap Getbitmap () {
Open Image Cache
View.setdrawingcacheenabled (TRUE);
The measure and layout methods must be called to successfully save the visual component's to PNG image file
Measuring View size
View.measure (Measurespec.makemeasurespec (0, measurespec.unspecified),
Measurespec.makemeasurespec (0, measurespec.unspecified));
Send location and dimensions to view and all of its sub-view
View.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());
Bitmap Bitmap = View.getdrawingcache ();
Android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) getlayoutparams ();
Params.width=bitmap.getwidth ();
Params.height=bitmap.getheight ();
Setlayoutparams (params);
return bitmap;
}
}
Android enables horizontal line display in portrait orientation