package com.fpt.drawlineview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class DrawCircleAdd extends View {
private Paint paint;
private Path path;
private int x1,y1,x2,y2,x3,y3,x4,y4;
private int[] move1,move2;
private int lengx,lengy,baselength,length;
public DrawCircleAdd(Context context) {
super(context);
initData();
}
public DrawCircleAdd(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initData();
}
public DrawCircleAdd(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
public DrawCircleAdd(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initData();
}
public void initData(){
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
path=new Path();
x1=60;y1=500;
x2=40;y2=700;
x3=240;y3=500;
x4=260;y4=700;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.reset();
path.moveTo(x1,y1);
path.lineTo(x2,y2);
path.lineTo(x4,y4);
path.lineTo(x3,y3);
path.close();
canvas.drawPath(path,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount()==2){//该判断放到down中,是监听不了的,只有这个地方可以监听
if (baselength==0) {
move1 = new int[]{(int) event.getX(0), (int) event.getY(0)};
move2 = new int[]{(int) event.getX(1), (int) event.getY(1)};
lengx = Math.abs(move2[0] - move1[0]);
lengy = Math.abs(move2[1] - move1[1]);
baselength = (int) Math.sqrt(lengx * lengx + lengy * lengy);
Log.e("event", "baselength:" + baselength);
}else {
move1 = new int[]{(int) event.getX(0), (int) event.getY(0)};
move2 = new int[]{(int) event.getX(1), (int) event.getY(1)};
lengx = Math.abs(move2[0] - move1[0]);
lengy = Math.abs(move2[1] - move1[1]);
length = (int) Math.sqrt(lengx * lengx + lengy * lengy);
length=length-baselength;
if (length<0){
updataCoorida(-1*3);//*3表示加速级别
}else {
updataCoorida(1*3);//*3表示加速级别
}
Log.e("event", "length:" + length);
baselength=0;//画龙点睛,精彩
}
}
break;
case MotionEvent.ACTION_UP:
length=0;
break;
}
return true;
}
public void updataCoorida(int number){
this.x1=x1-number;
this.x2=x2-number;
this.x3=x3+number;
this.x4=x4+number;
this.y1=y1-number;
this.y3=y3-number;
this.y2=y2+number;
this.y4=y4+number;
invalidate();//重新调用onDraw
}
}
package com.example.administrator.mystyleview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.util.AttributeSet;
public class MyView extends View {
private static final String TAG="MyView";
private String text;
private Rect mTextBounds;
private Paint paint;
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
text=ta.getString(R.styleable.MyView_my_text);
ta.recycle();
mTextBounds=new Rect();
paint=new Paint();
paint.setTextSize(40);
paint.getTextBounds(text,0,text.length(),mTextBounds);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 模式有3种:1、有精确的值了,例如100dp、match_parent,这都属于Exactly精确的;,2、at_most;3、
int width=0;
int height=0;
int Model=MeasureSpec.getMode(widthMeasureSpec);
int Size=MeasureSpec.getSize(widthMeasureSpec);
int Size_H=MeasureSpec.getSize(heightMeasureSpec);
int Mode_H=MeasureSpec.getMode(heightMeasureSpec);
Log.e(TAG, "onMeasure 模式打印: "+MeasureSpec.toString(widthMeasureSpec) );
if (Model==MeasureSpec.EXACTLY){
width=Size;
}
if (Model==MeasureSpec.AT_MOST){
width=mTextBounds.width()+getPaddingRight()+getPaddingLeft();
}
if (Mode_H==MeasureSpec.EXACTLY){
height=Size_H;
}
if (Mode_H==MeasureSpec.AT_MOST){
height=mTextBounds.height()+getPaddingTop()+getPaddingBottom();
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(text,getPaddingLeft(),mTextBounds.height()+getPaddingTop(),paint);
// canvas.drawText(text,0,mTextBounds.height()+getPaddingTop(),paint);
}
// public static void main(String[] args){
// System.out.print("hello world");
// if (true){
// throw new IllegalStateException("抛出异常");
// }
// }
}