MainActivity
package com.example.view1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=new Button(this);
findViewById(R.id.cv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ((CustemView)view).setPaint();
Toast.makeText(MainActivity.this,"aaaa",Toast.LENGTH_SHORT).show();
}
});
}
}
CustemView
package com.example.view1;
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 java.util.Random;
/**
* Created by tangxiaoying on 2017/9/28.
* onMeasure 测量方法
* onLayout摆放位置 在自定义viewGroup的时候需要使用
* onDarw 画布
*/
public class CustemView extends View {
Paint paint;
char[] array=new char[4];
Random r=new Random();
float downX=0;
//在类中直接使用new关键实例化控件时使用的构造方法
public CustemView(Context context) {
super(context);
init();
}
//在布局文件中创建控件,使用的是第二个构造方法
public CustemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
//canvas是画布对象
public CustemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
paint=new Paint();
//抗锯齿
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
}
//canvas是画布对象
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/* canvas.drawLine(0,0,100,100,paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(300,300,200,paint);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
canvas.drawBitmap(bitmap,100,100,paint);*/
for(int i=0;i<4;i++){
String a=r.nextInt(10)+"";
char[] temp=a.toCharArray();
array[i]=temp[0];
}
canvas.drawRect(100,100,300,200,paint);
paint.setColor(Color.YELLOW);
paint.setTextSize(80);
canvas.drawText(array,0,4,100,200,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
paint.setColor(Color.BLUE);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
float upX=event.getX();
if (upX>100&&upX<200){
paint.setColor(Color.RED);
postInvalidate();
}
break;
}
return super.onTouchEvent(event);
}
public void setPaint(){
paint.setColor(Color.BLACK);
invalidate();
}
}
Layout布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.view1.MainActivity">
<com.example.view1.CustemView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
实现效果图