当前位置:   article > 正文

android 自定义view的简单实例_安卓自定义view简单实例

安卓自定义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
<FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    xmlns:tools=" http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <com.example.androidmyview.v1.Myview 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

   <com.example.androidmyview.v3.LogicView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
</FrameLayout>
--------------------------------------------------------
使用xml文件解析自定义view


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号