android定义view实例,android 自定义view的简单实例

1,提取和封装 使用抽象方法

package com.example.androidmyview.v3;

import android.content.Context;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.view.View;

public abstract class BaseView extends View {

private mythread thread;

public BaseView(Context context) {

super(context);

}

public BaseView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public BaseView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

protected void onDraw(Canvas canvas) {

if (thread == null) {

thread = new mythread();

thread.start();

}else {

drawsub(canvas);

}

}

/**

* 绘制方法

* @param canvas

*/

protected  abstract void drawsub(Canvas canvas);

/**

* 逻辑运算

*/

protected abstract void Logic();

/**

* 自定义线程

* @author Administrator

*

*/

class mythread extends Thread {

@Override

public void run() {

while (true) {

Logic();

//从新绘制

postInvalidate();

try {

Thread.sleep(30);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

};

}

-----------------------------------------------

package com.example.androidmyview.v3;

import java.util.Random;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.RectF;

import android.util.AttributeSet;

public class LogicView extends BaseView{

private Paint paint;

private float x = 0;

private static String text ="LogicView";

private RectF rectf;

private float sweepAngle =0;

private Random random;

public LogicView(Context context) {

super(context);

}

public LogicView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public LogicView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

protected void drawsub(Canvas canvas) {

paint = new Paint();

paint.setTextSize(30);

rectf = new RectF(0,60,100,160);

canvas.drawText(text, x, 30, paint);

canvas.drawArc(rectf, 0, sweepAngle, true, paint);

}

@Override

protected void Logic() {

random  = new Random();

x += 3;

if (x>getWidth()) {

x=0-paint.measureText(text);

}

sweepAngle++;

if (sweepAngle>360) {

sweepAngle=0;

}

//产生随机数

int r = random.nextInt(256);

int g = random.nextInt(256);

int b = random.nextInt(256);

//颜色不断变化

paint.setARGB(255, r, g, b);

}

}

---------------------------------------------

布局文件采用framelayout

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:visibility="gone"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

--------------------------------------------------------

使用xml文件解析自定义view

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值