package com.lisen.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
public class Myview extends View {
// 画笔
Paint paint = new Paint();
// 颜色
int color = Color.RED;
// 屏幕的宽和高
private int with;
private int height;
// 圆心的位置
float centerx = 0;
float centery = 0;
// 圆的半径
float radius = 80;
float padding = 0;
public Myview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// 颜色
paint.setColor(color);
// 画圆
canvas.drawCircle(centerx, centery, radius, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
with = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
centerx = with / 2;
centery = height / 2;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int count = event.getPointerCount();
switch (count) {
case 1:
float gg = (event.getX() - centerx) * (event.getX() - centerx)
+ (event.getY() - centery) * (event.getY() - centery);
float kf = (float) Math.sqrt(gg);
if (kf <= radius) {
centerx = event.getX();
centery = event.getY();
invalidate();
}
break;
case 2:
// 计算两手之间的距离
float disX = Math.abs(event.getX(0) - event.getX(1));
float disY = Math.abs(event.getY(0) - event.getY(1));
// 两手之间的距离就是圆的直径
double sqrt = Math.sqrt(disX * disX + disY * disY);
// 计算圆的半径,重新刷新数据
radius = (float) sqrt / 2;
invalidate();
break;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
public class Myview extends View {
// 画笔
Paint paint = new Paint();
// 颜色
int color = Color.RED;
// 屏幕的宽和高
private int with;
private int height;
// 圆心的位置
float centerx = 0;
float centery = 0;
// 圆的半径
float radius = 80;
float padding = 0;
public Myview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// 颜色
paint.setColor(color);
// 画圆
canvas.drawCircle(centerx, centery, radius, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
with = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
centerx = with / 2;
centery = height / 2;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int count = event.getPointerCount();
switch (count) {
case 1:
float gg = (event.getX() - centerx) * (event.getX() - centerx)
+ (event.getY() - centery) * (event.getY() - centery);
float kf = (float) Math.sqrt(gg);
if (kf <= radius) {
centerx = event.getX();
centery = event.getY();
invalidate();
}
break;
case 2:
// 计算两手之间的距离
float disX = Math.abs(event.getX(0) - event.getX(1));
float disY = Math.abs(event.getY(0) - event.getY(1));
// 两手之间的距离就是圆的直径
double sqrt = Math.sqrt(disX * disX + disY * disY);
// 计算圆的半径,重新刷新数据
radius = (float) sqrt / 2;
invalidate();
break;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}