package jiacc.clockview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by user on 2017/1/19.
*/
public class ClockView extends View {
Paint mPaint;
Bitmap bitmap;
int height;
int width;
Matrix matrix;
int degree=0;
int unit=30;
public ClockView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.clock);
bitmap=toRoundBitMap(bitmap);
matrix=new Matrix();
matrix.postTranslate(-bitmap.getWidth()/4*3,0);
setClickable(true);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.i("jcc","onLayout");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("jcc","onMeasure");
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
if(widthMode==MeasureSpec.EXACTLY){
width=widthSize;
}else{
width=widthSize+getPaddingLeft()+getPaddingRight();
}
if(heightMode==MeasureSpec.EXACTLY){
height=heightSize;
}else{
height=heightSize+getPaddingLeft()+getPaddingRight();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("jcc","onDraw");
canvas.drawBitmap(bitmap,matrix,mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i("jcc","onSizeChanged");
}
float x,y,lastX,lastY,movedX,movedY;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX=event.getX();
lastY=event.getY();
break;
case MotionEvent.ACTION_MOVE:
x=event.getX();
y=event.getY();
matrix=new Matrix();
if(Math.abs(y-movedY)>Math.abs(x-movedX)&&Math.abs(movedY-y)>50){
degree=y-lastY>0?degree+unit:degree-unit;
}
matrix.postTranslate(-bitmap.getWidth()/4*3,0);
matrix.postRotate(degree,-bitmap.getWidth()/4,bitmap.getHeight()/2);
movedX=x;
movedY=y;
postInvalidate();
break;
case MotionEvent.ACTION_UP:
x=event.getX();
y=event.getY();
matrix=new Matrix();
if(Math.abs(y-lastY)>Math.abs(x-lastX)&&Math.abs(movedY-y)>30){
degree=y-lastY>0?degree+unit:degree-unit;
}
matrix.postTranslate(-bitmap.getWidth()/4*3,0);
matrix.postRotate(degree,-bitmap.getWidth()/4,bitmap.getHeight()/2);
postInvalidate();
break;
}
return super.onTouchEvent(event);
}
private Bitmap toRoundBitMap(Bitmap bitmap){
int width=bitmap.getWidth();
int height=bitmap.getHeight();
int r=0;
if(width>height){
r=height;
}else{
r=width;
}
Bitmap backgoundBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(backgoundBitmap);
Paint paint=new Paint();
paint.setAntiAlias(true);
RectF rectF=new RectF(0,0,r,r);
canvas.drawRoundRect(rectF,r/2,r/2,paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap,null,rectF,paint);
return backgoundBitmap;
}
}
实现滑动选择的效果,老式收音机的音量键