自定义View类
package com.lixinyang.myviews.Views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
/**
* author:Created by WangZhiQiang on 2017/12/26.
*/
public class MyLock extends View {
private Paint paint;
private Paint paint2;
private float length;
private int rX;
private int rY;
private int l;
private int r;
private int aqw;
private WindowManager wm;
public MyLock(Context context) {
this(context,null);
}
public MyLock(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyLock(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setColor(Color.RED);
paint2.setStrokeWidth(3);
wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
aqw = wm.getDefaultDisplay().getWidth();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
rX = (int) event.getRawX();
break;
case MotionEvent.ACTION_MOVE:
int mx = (int) event.getRawX();
int dx = mx - rX;
l = getLeft();
r = getRight();
l = l +dx;
r = r +dx;
if(l <0){
l =0;
r =getWidth();
}
if(r>aqw){
r=aqw;
l=aqw - getWidth();
}
layout(l,getTop(), r,getBottom());
rX = mx;
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(getContext(),""+aqw,Toast.LENGTH_LONG).show();
if(r==aqw){
Toast.makeText(getContext(),"恭喜你解锁成功",Toast.LENGTH_LONG).show();
}else{
l=0;
r=getWidth();
layout(l,getTop(),r,getBottom());
}
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.STROKE);
RectF oval=new RectF(0,5,50,70);
RectF oval2=new RectF(0,30,50,80);
canvas.drawArc(oval,175,190,false,paint);
canvas.drawRect(oval2,paint2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightmode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if(widthMode==MeasureSpec.EXACTLY){
width = widthSize;
}else{
width = 50+getPaddingLeft()+getPaddingRight();
}
if(heightmode==MeasureSpec.EXACTLY){
height = heightSize;
}else{
height = 70+getPaddingTop()+getPaddingBottom();
}
setMeasuredDimension(width,height);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:MyAttr="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lixinyang.myviews.MainActivity">
<com.lixinyang.myviews.Views.MyLock
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>