通过上一个博文的自定义View的编写后,发现代码其实挺复杂的,所有这里通过对代码进一步的优化,即用到了java的一个重要的特点:封装。通过对父类的方法进行封装,然后在子类继承后实现相应的函数即可,达到了代码的优化。
MainActivity.java
1 package com.example.myview; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 public class MainActivity extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 //setContentView(new Myview(this)); 14 } 15 }
activity_main.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 <com.example.myview.SubClass 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 /> 10 </FrameLayout>
封装后的TestClass.java
1 package com.example.myview; 2 3 import java.util.Random; 4 5 import android.content.Context; 6 import android.graphics.Canvas; 7 import android.graphics.Paint; 8 import android.graphics.RectF; 9 import android.util.AttributeSet; 10 import android.view.View; 11 12 public abstract class TestClass extends View{ 13 14 private MyThread thread; 15 16 public TestClass(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 } 19 20 public TestClass(Context context) { 21 super(context); 22 } 23 24 protected abstract void drawSub(Canvas canvas); 25 protected abstract void logic(); 26 27 protected void onDraw(Canvas canvas){ 28 if(thread==null){ 29 thread = new MyThread(); 30 thread.start(); 31 }else{ 32 drawSub(canvas); 33 } 34 } 35 36 37 class MyThread extends Thread{ 38 @Override 39 public void run() { 40 while(true){ 41 logic(); 42 postInvalidate(); 43 try { 44 Thread.sleep(30); 45 } catch (InterruptedException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 } 51 } 52 53 }
SubClass.java :继承自TestClass.java
1 package com.example.myview; 2 3 import java.util.Random; 4 5 import android.content.Context; 6 import android.graphics.Canvas; 7 import android.graphics.Paint; 8 import android.graphics.RectF; 9 import android.util.AttributeSet; 10 11 12 public class SubClass extends TestClass{ 13 14 private Paint paint = new Paint(); 15 private float rx = 0; 16 private RectF rectF = new RectF(0,60,100,160); 17 private float sweepAngel = 0; 18 Random rand = new Random(); 19 20 public SubClass(Context context) { 21 super(context); 22 } 23 24 public SubClass(Context context, AttributeSet attrs) { 25 super(context, attrs); 26 } 27 28 @Override 29 protected void drawSub(Canvas canvas) { 30 paint.setTextSize(30); 31 canvas.drawText("LogicView", rx, 30, paint); 32 33 canvas.drawArc(rectF,0,sweepAngel,true,paint); 34 } 35 36 @Override 37 protected void logic() { 38 rx++; 39 if(rx > getWidth()){ 40 rx = 0 - paint.measureText("LogicView"); 41 } 42 43 sweepAngel ++ ; 44 45 if(sweepAngel > 360){ 46 sweepAngel = 0; 47 } 48 int r = rand.nextInt(256); 49 int g = rand.nextInt(256); 50 int b = rand.nextInt(256); 51 paint.setARGB(255, r, g, b); 52 } 53 54 }
通过运行达到了一样的效果。
但是有些人会说,这样写的话不是更加复杂,而且代码增多了?
其实不然,通过对父类TestClass.java进行封装后,我们所要做的工作其实只有SubClass.java(继承自父类TestClass.java),然后实现类中的2个方法protected void drawSub(Canvas canvas)和protected void logic()即可,而线程的实现已经在父类TestClass.java中实现了,而不用再实现。当工作量很大的时候,这是相当有利的。
时间: 2024-12-27 22:25:41