一个旋钮的Demo

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;
    }

}

实现滑动选择的效果,老式收音机的音量键
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值