简单的图片旋转效果,第一种方法采用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);
}

}



图片旋转的两种方法_第1张图片

图片旋转的两种方法_第2张图片


更多相关文章

  1. android sdk 兼容低版本的处理方法
  2. 详细讲解Android的图片下载框架UniversialImageLoader之磁盘缓存
  3. OOM的出现及解决(加载图片)
  4. Android后端服务器的搭建方法
  5. 【Android 开发】:UI控件之 ImageView 实现适屏和裁剪图片的功能
  6. 保持Android手机屏幕长亮的方法
  7. Android 崩溃分析 方法论
  8. Android studio中正确引入so文件的方法
  9. Android底下多线程下载远程图片

随机推荐

  1. Android开发中 获取当前Android的年月日
  2. android handler和AsyncTask用法
  3. Android如何在测试程序中删除被测应用私
  4. Android 全局异常错误或崩溃捕捉
  5. 2.3.1 Android Studio使用记录——1.下载
  6. Android-sharedUserId数据权限
  7. Android(安卓)AOP 注解详解及简单使用实
  8. Android 模拟器中AVD路径的修改
  9. android设置背景平铺
  10. [Android Pro] 使用CursorLoader异步加载