Android Animation App-image rotation around fixed point

Source: Internet
Author: User

First, the function: realizes the picture rotates around the fixed point, the number of laps is random, Ontouch rotates.

Ii. Framework of Procedures:

Composition Function
Main activity:myactivity 1. Implement animation
2. Implement Ontouch
View:myview 1. Draw the breakout to the MyView
Third, the program source code:

Myview.java

Package Com.androids.kavinapps.myapplication;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.view.motionevent;import Android.view.view;import Android.view.animation.animation;import android.view.animation.rotateanimation;/** * Created by Administrator on 14-11-29.    */public class MyView extends view{//define roate animatioin public Animation manimationroate;    Define Bitmap object Bitmap mbitmap = null;        Public MyView (Context context) {super (context); Load Resource Mbitmap = ((bitmapdrawable) getresources (). Getdrawable (Com.androids.kavinapps.myapplication.r.drawa    ble.choujiang1)). Getbitmap ();        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        Paint mpaint = null; Draw pic Canvas.drawbitmap (mbitmap,0,40,null); }}


Myactivity.java

Package Com.androids.kavinapps.myapplication;import Android.app.activity;import Android.graphics.drawable.animationdrawable;import Android.os.bundle;import Android.os.message;import Android.os.handler;//handlerimport Android.util.log;import Android.view.menu;import Android.view.MenuItem;import Android.view.motionevent;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.rotateanimation;import Android.widget.button;import Android.widget.imageview;import    Android.widget.textview;public class MyActivity extends Activity {animationdrawable mAnimation1 = null;    int mrandom = 1;//random number MyView MyView;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        MyView = new MyView (this);        Setcontentview (MyView);        Mrandom = (int) (Math.random () *100);        if (mrandom%5==0) {mrandom = 5;        }else {mrandom = mrandom%5; } myview.manimationRoate = new Rotateanimation (0.0f, + (1800.0f +72*mrandom), Animation.relative_to_parent, 0.5f, animation.relative_to_        PARENT, 0.5f);        Set the time of Anim myView.mAnimationRoate.setDuration (3000);    MyView.mAnimationRoate.setFillAfter (TRUE);//After the animation is complete, the original myview.startanimation (myview.manimationroate) is not restored; }//oncreate public boolean ontouchevent (Motionevent event) {switch (Event.getaction ())} {case Motion                Event.ACTION_DOWN:mRandom = (int) (Math.random () *100);                if (mrandom%5==0) {mrandom = 5;                }else {mrandom = mrandom%5; } myview.manimationroate = new Rotateanimation (0.0f, + (1800.0f +72*mrandom), Animation.relative_to_parent,                0.5f, Animation.relative_to_parent, 0.5f);                MyView.mAnimationRoate.setDuration (3000); MyView.mAnimationRoate.setFillAfter (TRUE);//does not revert to the original myview.startanimation after the animation is complete (myView. manimationroate);        return true;    } return true; } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (r.menu.my, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Part IV Code Analysis

1. How to generate random numbers

Mrandom = (int) (Math.random () *100);
2. How to make the animation complete without restoring the original reload

MyView.mAnimationRoate.setFillAfter (TRUE);//The animation does not revert to its original state after completion

3. How to change the picture file under Drawable to bitmap

Mbitmap = ((bitmapdrawable) getresources (). Getdrawable (com.androids.kavinapps.myapplication.r.drawable.choujiang1 ). Getbitmap ();

December 9, 2014 14:41:34

Android Animation App-image rotation around fixed point

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.