android animation应用——图片绕固定点旋转

一、功能:实现将图片绕固定点旋转,圈数随机,onTouch后旋转。

二、程序框架:

组成功能
主Activity:MyActivity1.实现animation
2.实现onTouch
View       :MyView1.将突破绘制到MyView上
三、程序源代码:

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;//Handler
import 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;//随机数
    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);//动画完成后不恢复原状
        myView.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, +(1800.0f +72*mRandom), Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
                myView.mAnimationRoate.setDuration(3000);
                myView.mAnimationRoate.setFillAfter(true);//动画完成后不恢复原状
                myView.startAnimation(myView.mAnimationRoate);
                return true;
        }
        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);
    }
}

四、部分代码分析

1.如何产生随机数

mRandom = (int) (Math.random()*100);
2.如何使动画完成后,不恢复原装填

myView.mAnimationRoate.setFillAfter(true);//动画完成后不恢复原状

3.如何将drawable下的图片文件变为Bitmap

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

2014年12月9日14:41:34

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要在Android Studio中旋转图片,您可以使用以下步骤: 1. 将图片添加到您的项目中。 2. 打开您的布局文件,并将ImageView添加到其中。 3. 在ImageView中设置您要旋转图片。 4. 在ImageView中添加一个旋转动画。 5. 在动画中设置旋转的角度和持续时间。 6. 运行您的应用程序,您应该能够看到旋转图片。 以下是一个示例代码片段,可以帮助您实现这个功能: ``` // 获取ImageView ImageView imageView = (ImageView) findViewById(R.id.imageView); // 设置要旋转图片 imageView.setImageResource(R.drawable.my_image); // 创建旋转动画 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 设置动画持续时间 rotateAnimation.setDuration(1000); // 设置动画重复次数 rotateAnimation.setRepeatCount(Animation.INFINITE); // 开始动画 imageView.startAnimation(rotateAnimation); ``` 希望这可以帮助您旋转图片。 ### 回答2: 在Android开发中,图片旋转是非常常见的需求。Android Studio提供了多种方式实现图片旋转,以下是其中两种常用的方法。 1、Matrix实现旋转 Matrix类是Android中的一个矩阵类,可以用来实现各种图像操作,包括旋转、缩放、翻转等。在Android Studio中,可以使用Matrix来旋转图片: 首先,定义一个Matrix对象 Matrix matrix = new Matrix(); 然后,调用Matrix的rotate方法来旋转图片 matrix.postRotate(degrees); 最后,将Matrix对象应用图片Bitmap newBitmap = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 其中,degrees表示旋转角度,bitmap是待旋转的原始图片对象,newBitmap旋转后的新图片对象。 2、Animation实现旋转 另一个实现图片旋转的方法是使用Animation动画。在Android Studio中,可以使用RotateAnimation类来实现图片旋转: 首先,定义一个RotateAnimation对象 RotateAnimation rotateAnimation = new RotateAnimation( fromDegrees, toDegrees, pivotX, pivotY); 参数说明: fromDegrees:起始旋转角度 toDegrees:终止旋转角度 pivotX:X方向的旋转中心点 pivotY:Y方向的旋转中心点 然后,将RotateAnimation对象应用到ImageView上 imageView.startAnimation(rotateAnimation); 其中,imageView是待旋转的ImageView对象。 总的来说,这两种方法都可以实现图片旋转,具体使用哪一个方法,需要根据实际需要和效率来选择。 ### 回答3: Android Studio是一款常用的Android开发集成环境,可以帮助开发者开发高质量的Android应用程序。在Android应用程序中,经常需要通过代码对图片进行操作,其中就包括图片旋转。接下来我们将讨论如何在Android Studio中对图片进行旋转操作。 1.使用Matrix对象进行图片旋转 首先,在XML布局文件中添加一个ImageView组件。代码如下: <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image"/> 要在代码中旋转此图像,我们需要使用一个Matrix对象。创建一个Matrix对象,并将其应用于ImageView的图像,以实现旋转。代码如下: ImageView imageView = findViewById(R.id.imageView); Matrix matrix = new Matrix(); imageView.setScaleType(ImageView.ScaleType.MATRIX); matrix.postRotate(90, imageView.getDrawable().getBounds().width() / 2, imageView.getDrawable().getBounds().height() / 2); imageView.setImageMatrix(matrix); 这段代码将图像旋转90度,并将其应用于ImageView中使用的Matrix对象。 2.使用Bitmap对象进行图片旋转 另一种方法是使用Bitmap对象进行图片旋转。使用此方法可以在后台进行旋转,而不必在主线程中等待。代码如下: Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image); Matrix matrix = new Matrix(); matrix.postRotate(90); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true); 此代码将图像通过Bitmap对象旋转了90度,并最终生成了一个旋转后的Bitmap对象rotatedBitmap。 总结 在Android Studio中,可以使用Matrix对象或Bitmap对象旋转图像。使用Matrix对象需要在主线程中等待,而使用Bitmap对象可以在线程后台进行旋转,因此更适合较大的图像。但是无论使用哪一种方法,都需要注意在旋转图片时,避免修改原始图像。为了避免这种情况,最好保存原始图像,并对副本进行操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值