图片可控旋转效果

经常可以看到QQ音乐播放背景有个旋转的光盘在转动,想了下其实就是一个可以控制播放与停止的图片在旋转,我写了一个demo和大家分享下:
 CircleImageView.class
package com.liw.imagerevolve;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
 * 自定义一个ImageView,这里是创建一个圆
 * @author liw
 *
 */
public class CircleImageView extends ImageView {


private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;//按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者大于相应的视图的维度


private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
private static final int COLORDRAWABLE_DIMENSION = 1;


private static final int DEFAULT_BORDER_WIDTH = 0;
private static final int DEFAULT_BORDER_COLOR = Color.BLACK;


private final RectF mDrawableRect = new RectF();
private final RectF mBorderRect = new RectF();


private final Matrix mShaderMatrix = new Matrix();
private final Paint mBitmapPaint = new Paint();
private final Paint mBorderPaint = new Paint();


private int mBorderColor = DEFAULT_BORDER_COLOR;
private int mBorderWidth = DEFAULT_BORDER_WIDTH;


private Bitmap mBitmap;
private BitmapShader mBitmapShader;
private int mBitmapWidth;
private int mBitmapHeight;


private float mDrawableRadius;
private float mBorderRadius;


private boolean mReady;
private boolean mSetupPending;


private float mRotateDegree = 5;//旋转角度
private Camera mCamera = new Camera();


public void setDegree(float degree) {
mRotateDegree = degree;
postInvalidate();
}


public float getDegree() {
return mRotateDegree;
}


public CircleImageView(Context context) {
super(context);
}


public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setScaleType(SCALE_TYPE);


mBorderWidth = 0;
mBorderColor = 0;


mReady = true;


if (mSetupPending) {
setup();
mSetupPending = false;
}
}


@Override
public ScaleType getScaleType() {
return SCALE_TYPE;
}


@Override
public void setScaleType(ScaleType scaleType) {
if (scaleType != SCALE_TYPE) {
throw new IllegalArgumentException(String.format(
"ScaleType %s not supported.", scaleType));
}
}


@Override
protected void onDraw(Canvas canvas) {
if (getDrawable() == null) {
return;
}
Matrix matrix = canvas.getMatrix();
matrix.postRotate(mRotateDegree, getWidth() / 2, getHeight() / 2);
canvas.concat(matrix);


canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,
mBitmapPaint);
if (mBorderWidth != 0) {
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,
mBorderPaint);
}
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setup();
}


public int getBorderColor() {
return mBorderColor;
}


public void setBorderColor(int borderColor) {
if (borderColor == mBorderColor) {
return;
}


mBorderColor = borderColor;
mBorderPaint.setColor(mBorderColor);
invalidate();
}


public int getBorderWidth() {
return mBorderWidth;
}


public void setBorderWidth(int borderWidth) {
if (borderWidth == mBorderWidth) {
return;
}


mBorderWidth = borderWidth;
setup();
}


@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
mBitmap = bm;
setup();
}


@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
mBitmap = getBitmapFromDrawable(drawable);
setup();
}


@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}


private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
return null;
}


if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}


try {
Bitmap bitmap;


if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,
COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), BITMAP_CONFIG);
}


Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
return null;
}
}


private void setup() {
if (!mReady) {
mSetupPending = true;
return;
}


if (mBitmap == null) {
return;
}


mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);


mBitmapPaint.setAntiAlias(true);
mBitmapPaint.setShader(mBitmapShader);


mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStrokeWidth(mBorderWidth);


mBitmapHeight = mBitmap.getHeight();
mBitmapWidth = mBitmap.getWidth();


mBorderRect.set(0, 0, getWidth(), getHeight());
mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2,
(mBorderRect.width() - mBorderWidth) / 2);


mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width()
- mBorderWidth, mBorderRect.height() - mBorderWidth);
mDrawableRadius = Math.min(mDrawableRect.height() / 2,
mDrawableRect.width() / 2);


updateShaderMatrix();
invalidate();
}


private void updateShaderMatrix() {
float scale;
float dx = 0;
float dy = 0;


mShaderMatrix.set(null);


if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width()
* mBitmapHeight) {
scale = mDrawableRect.height() / (float) mBitmapHeight;
dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
} else {
scale = mDrawableRect.width() / (float) mBitmapWidth;
dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
}


mShaderMatrix.setScale(scale, scale);
mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth,
(int) (dy + 0.5f) + mBorderWidth);


mBitmapShader.setLocalMatrix(mShaderMatrix);
}


}


  activity_main.xml和activity_main_two.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#ffffff"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="300dp"  
  10.         android:background="#ffffff"  
  11.         android:gravity="center" >  
  12.   
  13.         <FrameLayout  
  14.             android:layout_width="200dp"  
  15.             android:layout_height="200dp"  
  16.             android:background="@drawable/image_one" >  
  17.   
  18.             <com.dengyy.imagerevolve.CircleImageView  
  19.                 android:id="@+id/img_test"  
  20.                 android:layout_width="150dp"  
  21.                 android:layout_height="150dp"  
  22.                 android:layout_gravity="center"  
  23.                 android:src="@drawable/image_two" />  
  24.         FrameLayout>  
  25.     LinearLayout>  
  26.   
  27.     <LinearLayout  
  28.         android:layout_width="match_parent"  
  29.         android:layout_height="match_parent"  
  30.         android:orientation="horizontal" >  
  31.   
  32.         <Button  
  33.             android:id="@+id/start"  
  34.             android:layout_width="0dp"  
  35.             android:layout_height="match_parent"  
  36.             android:layout_weight="1"  
  37.             android:text="启动" />  
  38.   
  39.         <Button  
  40.             android:id="@+id/stop"  
  41.             android:layout_width="0dp"  
  42.             android:layout_height="match_parent"  
  43.             android:layout_weight="1"  
  44.             android:text="停止" />  
  45.     LinearLayout>  
  46.   
  47. LinearLayout>  
  48.   
  49.   
  50. ------------------------------------------  
  51. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  52.     android:layout_width="match_parent"  
  53.     android:layout_height="match_parent"  
  54.     android:background="#ffffff"  
  55.     android:orientation="vertical" >  
  56.   
  57.     <LinearLayout  
  58.         android:layout_width="match_parent"  
  59.         android:layout_height="300dp"  
  60.         android:background="#ffffff"  
  61.         android:gravity="center" >  
  62.   
  63.         <FrameLayout  
  64.             android:layout_width="200dp"  
  65.             android:layout_height="200dp"  
  66.             android:background="@drawable/image_one" >  
  67.   
  68.             <ImageView  
  69.                 android:id="@+id/img_test"  
  70.                 android:layout_width="150dp"  
  71.                 android:layout_height="150dp"  
  72.                 android:layout_gravity="center"  
  73.                 android:src="@drawable/image_two" />  
  74.         FrameLayout>  
  75.     LinearLayout>  
  76.   
  77.     <LinearLayout  
  78.         android:layout_width="match_parent"  
  79.         android:layout_height="match_parent"  
  80.         android:orientation="horizontal" >  
  81.   
  82.         <Button  
  83.             android:id="@+id/start"  
  84.             android:layout_width="0dp"  
  85.             android:layout_height="match_parent"  
  86.             android:layout_weight="1"  
  87.             android:text="启动" />  
  88.   
  89.         <Button  
  90.             android:id="@+id/stop"  
  91.             android:layout_width="0dp"  
  92.             android:layout_height="match_parent"  
  93.             android:layout_weight="1"  
  94.             android:text="停止" />  
  95.     LinearLayout>  
  96.   
  97. LinearLayout>  


    MainActivity.class
    package com.liw.imagerevolve;


    import com.liw.imagerevolve.R;


    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.renderscript.Sampler.Value;
    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 MainActivity extends Activity {
    private CircleImageView imageView;
    private boolean isStart = true;
    private Thread th;
    private Button start;
    private Button stop;
    TextView tv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init1();




       }





    private void init1() {
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    imageView = (CircleImageView) findViewById(R.id.img_test);


    final CircleThread st = new CircleThread();
    th = new Thread(st);


    start.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    if (isStart) {
    try {th.start();} 
    catch (IllegalThreadStateException e) {}
    } else {
    th = new Thread(st);
    th.start();
    isStart = true;
    }
    }
    });


    stop.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    st.stopThread();
    }
    });
    }


    private void init2() {
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    final ImageView View = (ImageView) findViewById(R.id.img_test);


    /** 设置旋转动画 */
    final RotateAnimation animation = new RotateAnimation(0f, 360f,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    0.5f);
    animation.setDuration(3000);// 设置动画持续时间


    start.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    View.setAnimation(animation);
    animation.startNow();
    }
    });


    stop.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    animation.cancel();
    }
    });
    }


    private class CircleThread implements Runnable {


    public void stopThread() {
    isStart = false;
    }


    @Override
    public void run() {
    while (isStart) {
    imageView.setDegree(imageView.getDegree() + 3);
    try {
    Thread.sleep(20);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }


    }
    }


    }








你可能感兴趣的:(图片可控旋转效果)