package com.qianqian.day4;
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 v) {
// ((CustemView)v).setPaint();
Toast.makeText(MainActivity.this, "dddd", Toast.LENGTH_SHORT).show();
}
});
}
}
package com.qianqian.day4;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
/**
* 作用:测量方法
* 摆放的位置--在自定义viewgroup的时候需要使用
* 用来绘制
* 作者:李倩 on 2017/9/28 09:30
*/
public class CurrView extends View{
Paint paint;
char[] array=new char[4];
Random r=new Random();
float downX=0;
//在类中直接使用new关键字实例化控件时使用的构造方法
public CurrView(Context context) {
super(context);
init();
}
//在布局文件中创建的控件 使用的是第二个构造方法
public CurrView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CurrView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint=new Paint();
//抗锯齿
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
}
//画布
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/* //直线
canvas.drawLine(0,0,100,100,paint);
//圆,空心圆
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(100,100,50,paint);
//加载图片
Bitmap bit= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
canvas.drawBitmap(bit,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:
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);
invalidate();
}
break;
}
return super.onTouchEvent(event);
}
public void setPaint(){
paint.setColor(Color.BLACK);
invalidate();
}
}
<?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.qianqian.day4.MainActivity">
<com.qianqian.day4.CurrView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>