您的位置:首页 > 其它

图片旋转的两种方法

2012-12-19 19:09 232 查看
简单的图片旋转效果,第一种方法采用tween动画实现,调用customView1(); 第二种采用了自定义类,然后通过handlermessage来实现。

TestSimpleActivity.java

package com.goso.ui;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.LinearInterpolator;

import android.view.animation.RotateAnimation;

import android.widget.Button;

import android.widget.EditText;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

public class TestSimpleActivity extends Activity{

EditText editText;

ImageView imageView;

RotateView mRotateView;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

customView1();

}

private void customView2(){

LinearLayout lv = new LinearLayout(this);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.MATCH_PARENT);

lv.setOrientation(LinearLayout.VERTICAL);

lv.setLayoutParams(params);

Button btn = new Button(this);

btn.setText("更新");

LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(200,

LinearLayout.LayoutParams.WRAP_CONTENT);

lv.addView(btn, btnParams);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

mRotateView.startRotate();

}

});

Button btn_stop = new Button(this);

btn_stop.setText("停止");

LinearLayout.LayoutParams stopParams = new LinearLayout.LayoutParams(200,

LinearLayout.LayoutParams.WRAP_CONTENT);

lv.addView(btn_stop, stopParams);

btn_stop.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

mRotateView.stopRotate();

}

});

mRotateView = new RotateView(this);

LinearLayout.LayoutParams myViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

lv.addView(mRotateView, myViewParams);

setContentView(lv);

}

private void customView1(){

LinearLayout lv = new LinearLayout(this);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.MATCH_PARENT);

lv.setOrientation(LinearLayout.VERTICAL);

lv.setLayoutParams(params);

Button btn = new Button(this);

btn.setText("更新");

LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(200,

LinearLayout.LayoutParams.WRAP_CONTENT);

lv.addView(btn, btnParams);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

// editText.setText("Hello Android!");

// Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in);

// editText.setAnimation(anim);

imageView.setVisibility(View.VISIBLE);

Animation rotateAnimation = new RotateAnimation(0f, +360f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

LinearInterpolator lir = new LinearInterpolator();

rotateAnimation.setDuration(1000);

rotateAnimation.setRepeatCount(-1);

rotateAnimation.setInterpolator(lir);

imageView.startAnimation(rotateAnimation);

}

});

Button btn_stop = new Button(this);

btn_stop.setText("停止");

LinearLayout.LayoutParams stopParams = new LinearLayout.LayoutParams(200,

LinearLayout.LayoutParams.WRAP_CONTENT);

lv.addView(btn_stop, stopParams);

btn_stop.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

imageView.clearAnimation();

imageView.setVisibility(View.INVISIBLE);

}

});

FrameLayout frameLayout = new FrameLayout(this);

FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,

FrameLayout.LayoutParams.MATCH_PARENT);

frameLayout.setLayoutParams(frameParams);

ImageView bgImageView = new ImageView(this);

bgImageView.setImageResource(R.drawable.record_circle);

FrameLayout.LayoutParams bgimageParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,

FrameLayout.LayoutParams.WRAP_CONTENT);

frameLayout.addView(bgImageView, bgimageParams);

imageView = new ImageView(this);

imageView.setImageResource(R.drawable.record_rotate);

FrameLayout.LayoutParams imageParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,

FrameLayout.LayoutParams.WRAP_CONTENT);

frameLayout.addView(imageView, imageParams);

imageView.setVisibility(View.INVISIBLE);

lv.addView(frameLayout);

setContentView(lv);

}

}

RotateView.java:

package com.goso.ui;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.drawable.Drawable;

import android.os.Handler;

import android.view.View;

public class RotateView extends View{

private Drawable mBgDrawable;

private Drawable mRotateDrawable;

public RotateView(Context context) {

super(context);

// TODO Auto-generated constructor stub

init(context);

}

private void init(Context context){

mBgDrawable = context.getResources().getDrawable(R.drawable.record_circle);

mRotateDrawable = context.getResources().getDrawable(R.drawable.record_rotate);

mRotateWidth = mRotateDrawable.getIntrinsicWidth();

mRotateHeight = mRotateDrawable.getIntrinsicHeight();

mRotateDrawable.setBounds(0, 0, mRotateWidth, mRotateHeight);

setBackgroundDrawable(mBgDrawable);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// TODO Auto-generated method stub

//super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int width = mBgDrawable.getIntrinsicWidth();

int height = mBgDrawable.getIntrinsicHeight();

setMeasuredDimension(width, height);

}

private static final int step = 15;

private int mRotateWidth;

private int mRotateHeight;

private int angle = 0;

private boolean mRun;

private Handler mHandler = new Handler(){

public void handleMessage(android.os.Message msg) {

angle = (angle + step) % 360;

invalidate();

if(mRun){

mHandler.sendMessageDelayed(mHandler.obtainMessage(), 10L);

}

};

};

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

if(mRun) {

canvas.rotate(angle, mRotateWidth / 2, mRotateHeight / 2);

mRotateDrawable.draw(canvas);

}

}

public void stopRotate(){

mRun = false;

}

public void startRotate(){

mRun = true;

mHandler.sendMessageDelayed(mHandler.obtainMessage(), 10L);

}

}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: