自定义View用法
- 效果图
- 布局文件
- 重写view
- Java代码
- 效果图二
- 布局文件
- 重写view
- Java代码
- 效果图三
- 布局文件
- 重写view
- Java代码
效果图
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.Unit4.User_DefinedActivity">
<bw.com.Unit4.CoustomView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
重写view
package bw.com.Unit4;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class CoustomView extends View {
Paint paint;
public CoustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
private void initPaint() {
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(0,0,100,100,paint);
RectF rectF1 = new RectF(100,100,200,200);
canvas.drawRect(rectF1,paint);
canvas.drawCircle(250,250,70,paint);
RectF rectF2 = new RectF(350,350,400,400);
canvas.drawArc(rectF2,0,100,true,paint);
RectF rectF3 = new RectF(100,200,500,500);
canvas.drawOval(rectF3,paint);
}
}
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import bw.com.Unit1.R;
public class User_DefinedActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user__defined);
CoustomView view = findViewById(R.id.my_view);
}
}
效果图二
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.Unit4.User_DefinedActivity">
<bw.com.Unit4.User_View
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
重写view
package bw.com.Unit4;
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.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
public class User_View extends View implements Runnable{
private Context context;
private Paint paint;
private int height;
private int width;
private int radiu;
public User_View(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
initPaint();
}
private void initPaint() {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
paint.setColor(Color.BLUE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width/2,height/2,radiu,paint);
}
@Override
public void run() {
while (true){
if (radiu < 500){
radiu +=20;
}else{
radiu = 20;
}
postInvalidate();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Java代码
package bw.com.Unit4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import bw.com.Unit1.R;
public class User_DefinedActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user__defined);
User_View view = findViewById(R.id.my_view);
new Thread(view).start();
}
}
效果图三
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.Unit4.User_DefinedActivity">
<bw.com.Unit4.ElectricFanView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
重写view
package bw.com.Unit4;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class ElectricFanView extends View implements Runnable{
private Paint paintR;
private Paint paintB;
private Paint paintY;
private Paint paintG;
private int height;
private int width;
private int sweepAngleB;
private int sweepAngleR;
private int sweepAngleY;
public ElectricFanView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
private void initPaint() {
paintB = new Paint();
paintB.setAntiAlias(true);
paintB.setStyle(Paint.Style.FILL);
paintB.setStrokeWidth(20);
paintB.setColor(Color.BLUE);
paintR = new Paint();
paintR.setAntiAlias(true);
paintR.setStyle(Paint.Style.FILL);
paintR.setStrokeWidth(20);
paintR.setColor(Color.RED);
paintY = new Paint();
paintY.setAntiAlias(true);
paintY.setStyle(Paint.Style.FILL);
paintY.setStrokeWidth(20);
paintY.setColor(Color.YELLOW);
paintG = new Paint();
paintG.setAntiAlias(true);
paintG.setStyle(Paint.Style.FILL);
paintG.setStrokeWidth(10);
paintG.setColor(Color.BLACK);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.height = h;
this.width = w;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectFG = new RectF(width/2-50,height/2-125,width/2+50,height/2-75);
canvas.drawOval(rectFG,paintG);
canvas.drawLine(width/2,height/2,width/2,height/2-100,paintG);
RectF rectFB = new RectF(-(width/4),-(width/4),(width/4),(width/4));
canvas.translate(width/2,height/2);
canvas.drawArc(rectFB,sweepAngleB,30,true,paintB);
canvas.drawArc(rectFB,sweepAngleR,30,true,paintR);
canvas.drawArc(rectFB,sweepAngleY,30,true,paintY);
}
@Override
public void run() {
while (true){
sweepAngleB+=1;
sweepAngleR = sweepAngleB+120;
sweepAngleY = sweepAngleR+120;
postInvalidate();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Java代码
package bw.com.Unit4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import bw.com.Unit1.R;
public class User_DefinedActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user__defined);
ElectricFanView view = findViewById(R.id.my_view);
new Thread(view).start();
}
}