Auto-Moving ImageView

Source: Internet
Author: User

The picture will slowly move to the left, after the end, and then loop

Actually this effect and the screen background picture effect is similar, the screen background image is with the sliding to slowly scroll, this is oneself each n seconds starts to move. The way to do this is naturally a custom control. This inheritance is ImageView, the implementation of the principle is to use a handler to update the x-coordinate, not 10 milliseconds to notify the next handler, let it calculate the value of the x-coordinate. The invalidate () method is then used to inform the redraw of the picture so that the OnDraw () method can be called multiple times.

Myimageview.java

 Packagecom.kale.imageview03;ImportJava.util.Timer;ImportJava.util.TimerTask;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.AttributeSet;ImportAndroid.util.DisplayMetrics;ImportAndroid.util.Log;ImportAndroid.widget.ImageView;/** * @authorWulianghuan * This class is a custom ImageView for displaying the background picture and showing the background picture to the move effect **/ Public classMyimageviewextendsimageview{PrivateBitmap back;//Background picture Resources    PrivateBitmap Mbitmap;//Generate Bitmap    Private DoubleStartX = 0;//move start x coordinate//The constructor must have context,attributeset these two arguments, otherwise the parent class cannot invoke the     PublicMyimageview (Context context,attributeset attributeset) {Super(context, attributeset); //because it is not an activity subclass, you can only get screen information through Displaymetrics.Displaymetrics DM =getresources (). Getdisplaymetrics (); //Screen Width        intScreenWidth =Dm.widthpixels; //Screen Height        intScreenHeight =Dm.heightpixels; back=Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.ROOTBLOCK_DEFAULT_BG); //stretch the picture to three times times the width of the screenMbitmap = Bitmap.createscaledbitmap (back, screenwidth*3, ScreenHeight,true); //the image is automatically moved to the left and the head is moved again        FinalHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {//determines whether the value of the message is 1, if it is, it is sent by my program .                if(Msg.what = = 1) {log.i ("TAG", "-----" +StartX); //if the head is over, set the x-coordinate directly to 0. This effect is a little abrupt, probably know a principle can                    if(StartX <=-80) {StartX= 0; }                    Else                    {                        //If you don't have a head, you reduce the coordinates.StartX-= 0.09; }} invalidate ();//after the coordinates are updated, redraw. The OnDraw () method is called            }        }; NewTimer (). Schedule (NewTimerTask () {@Override Public voidrun () {//The value of the sent message is 1,handler to determine the value, which is 1 execution. Handler.sendemptymessage (1); }            //no delay, 10 milliseconds loop at a time. }, 0, 10); } @Override Public voidOnDraw (canvas canvas) {LOG.I ("TAG", "-----onDraw"); Bitmap BITMAP2=Bitmap.createbitmap (MBITMAP); Canvas.drawbitmap (Mbitmap, (float) StartX, 0,NULL); }}

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.