Android animation application-rotating images around Fixed Points

Source: Internet
Author: User

Android animation application-rotating images around Fixed Points

I. function: Rotate images around fixed points, with Random Number of circles and rotation after onTouch.

Ii. Program Framework:

Composition Function
Main Activity: MyActivity 1. implement animation
2. onTouch implementation
View: MyView 1. Draw the breakthrough to MyView.
3. 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.drawable.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, + (18000000f + 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); // The animation is not restored to its original state. startAnimation (myView. mAnimationRoate);} // onCreate public boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: mRandom = (int) (Math. random () * 100); if (mRandom % 5 = 0) {mRandom = 5;} else {mRandom = mRandom % 5;} myView. mAnimationRoate = new RotateAnimation (0.0f, + (18000000f + 72 * mRandom), Animation. RELATIVE_TO_PARENT, 0.5f, Animation. RELATIVE_TO_PARENT, 0.5f); myView. mAnimationRoate. setDuration (3000); myView. mAnimationRoate. setFillAfter (true); // The animation is not restored to its original state. startAnimation (myView. mAnimationRoate); return true;} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action 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 will // 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 );}}

Iv. Code Analysis

1. How to generate a random number

mRandom = (int) (Math.random()*100);
2. How to Make the animation complete, do not restore the original fill

MyView. mAnimationRoate. setFillAfter (true); // The animation is not restored

3. How to change the image file under drawable to Bitmap

mBitmap = ((BitmapDrawable)getResources().getDrawable(com.androids.kavinapps.myapplication.R.drawable.choujiang1)).getBitmap();

December 9, 2014 14:41:34

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.