自定义View
程序员文章站
2022-05-30 20:28:52
...
1.自定义View画钟表
新建一个类继承View
package com.example.shinelon.diyview.tools;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
public class viewdemo1 extends View{
//重写有两个参数的方法
public viewdemo1(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
//重写onDraw方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//创建一支画笔,设置画笔的颜色,风格(画笔实心或空心)
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
//设置画笔的粗细(如果风格为Fill,则无效)
paint.setStrokeWidth(10);
int radius = 300;
//画布调用画圆的方法,参数为x,y确定圆心,radius半径,用的画笔
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, 20, paint);
//画布调用画直线的方法,参数为x,y确定起点,后两个x,y确定终点
canvas.drawLine(getWidth()/2,getHeight()/2,getWidth()/2+100,getHeight()/2+100,paint);
for(int i = 1;i<=12;i++){
//保存当前画布,保存canvas的状态,save方法后可以进行旋转,平移 缩放等操作
canvas.save();
//画布旋转:三个参数分别表示旋转的角度,旋转中心点的X坐标和Y坐标
canvas.rotate(30*i,getWidth()/2,getHeight()/2);
canvas.drawLine(getWidth()/2,getHeight()-radius,getWidth()/2,getHeight()-radius+20,paint);
paint.setStyle(Paint.Style.FILL);
canvas.drawText(i+"",getWidth()/2-10,getHeight()/2-radius+40,paint);
//回复画布的位置,不改变之前对画布的操作
canvas.restore();
}
}
}
在布局文件中加载这个自定义View
activity_main.xml
<com.example.shinelon.diyview.tools.viewdemo1
android:layout_width="match_parent"
android:layout_height="match_parent" />
2.自定义Button,实现返回上一页功能
package com.example.shinelon.diybutton;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
//自定义一个类,继承Button
public class DIYButton extends android.support.v7.widget.AppCompatButton {
Activity myActivity;
public DIYButton(final Context context, AttributeSet attrs) {
super(context, attrs);
myActivity = (Activity) context;
//this指当前类 (当前类继承了button,可以理解为当前类就是一个button)
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(myActivity, "hahaha", Toast.LENGTH_SHORT).show();
myActivity.finish();
//context不能直接调用finish方法,要强转为Activity
//((Activity) context).finish();
}
});
}
}
同样记得在布局文件中加载
<com.example.shinelon.diybutton.DIYButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击返回"/>
3.自定义ImageView,给图片添加相框
package com.example.shinelon.diybutton;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
//新建一个类,继承ImageView
public class ImageView1 extends android.support.v7.widget.AppCompatImageView {
//实现点击ImageView,跳转的另一个页面,与点击Button结束页面类似
Activity Youractivity;
public ImageView1(final Context context, AttributeSet attrs) {
super(context, attrs);
Youractivity = (Activity) context;
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,SecondActivity.class);
Youractivity.startActivity(intent);
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
//得到画布的边框
Rect clipBounds = canvas.getClipBounds();
//画矩形,五个参数分别表示矩形左 上 右 下 边距离手机上边框和左边框的距离,详解见百度
//canvas.drawRect(50,50,300,150,paint);
canvas.drawRect(clipBounds,paint);
}
}
记得在布局文件中加载
整体效果