您的位置:首页 > 其它

自定义View(三)——封装自定义View,通过封装类实现文字滚动、画圆

2016-03-12 19:02 393 查看
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cctvjiatao.customview"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
com.cctvjiatao.customview.MainActivity

package com.cctvjiatao.customview;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

}
activity_main.xml

<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.cctvjiatao.customview.v1.CustomView1
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:visibility="gone" >
</com.cctvjiatao.customview.v1.CustomView1>

<com.cctvjiatao.customview.v2.CustomView1
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
</com.cctvjiatao.customview.v2.CustomView1>

<com.cctvjiatao.customview.v2.CustomView2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
</com.cctvjiatao.customview.v2.CustomView2>

<com.cctvjiatao.customview.v2.CustomView3
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
</com.cctvjiatao.customview.v2.CustomView3>

<com.cctvjiatao.customview.v3.CustomView1
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
</com.cctvjiatao.customview.v3.CustomView1>

<com.cctvjiatao.customview.v3.CustomView2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</com.cctvjiatao.customview.v3.CustomView2>

<com.cctvjiatao.customview.v4.CustomView1
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.cctvjiatao.customview.v4.CustomView1>

<com.cctvjiatao.customview.v4.CustomView2
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.cctvjiatao.customview.v4.CustomView2>

</FrameLayout>
com.cctvjiatao.customview.v4.BaseView

<pre name="code" class="java">package com.cctvjiatao.customview.v4;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

/**
* @作者: jiatao
* @修改时间:2016-3-12 下午5:30:22
* @包名:com.cctvjiatao.customview.v4
* @文件名:BaseView.java
* @版权声明:www.cctvjiatao.com
* @功能: 自定义View的封装类
*/
public abstract class BaseView extends View {

private MyThread thread;
private boolean isRunning = true;
private long time = 30;

public BaseView(Context context) {
super(context);
}

public BaseView(Context context, AttributeSet attrs) {
super(context, attrs);
}

/**
* 画空间
*/
protected abstract void drawSub(Canvas canvas);
/**
* 花时间
*/
protected abstract void drawLogic();

@Override
protected final void onDraw(Canvas canvas) {//增加final限制,不允许子类修改此方法
if(thread == null){
thread = new MyThread();
thread.start();
}else{
drawSub(canvas);
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
isRunning = false;
}

class MyThread extends Thread{
@Override
public void run() {
while(isRunning){
drawLogic();
postInvalidate();
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}



com.cctvjiatao.customview.v4.CustomView1

package com.cctvjiatao.customview.v4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

/**
* @作者: jiatao
* @修改时间:2016-3-12 下午6:21:38
* @包名:com.cctvjiatao.customview.v4
* @文件名:CustomView1.java
* @版权声明:www.cctvjiatao.com
* @功能: 通过继承自定义View的封装类来实现文字滚动
*/
public class CustomView1 extends BaseView {

private float move_x = 0;
private Paint paint = new Paint();

public CustomView1(Context context) {
super(context);
}

public CustomView1(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void drawSub(Canvas canvas) {
paint.setTextSize(30);
canvas.drawText("cctvjiatao", move_x, 30, paint);
}

@Override
protected void drawLogic() {
move_x += 3;
if (move_x > getWidth()) {//如果文字滑出屏幕
move_x = 0 - paint.measureText("cctvjiatao");
}
}
}


com.cctvjiatao.customview.v4.CustomView2

package com.cctvjiatao.customview.v4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
* @作者: jiatao
* @修改时间:2016-3-12 下午6:45:21
* @包名:com.cctvjiatao.customview.v4
* @文件名:CustomView2.java
* @版权声明:www.cctvjiatao.com
* @功能: 通过继承自定义的View类实现画圆
*/
public class CustomView2 extends BaseView {

private RectF rectf = new RectF(100,100,300,300);
private Paint paint = new Paint();
private long sweepAngle = 0;

public CustomView2(Context context) {
super(context);
}

public CustomView2(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void drawSub(Canvas canvas) {
canvas.drawArc(rectf, 0, sweepAngle, true, paint);
}

@Override
protected void drawLogic() {
sweepAngle++;
if(sweepAngle > 360){
sweepAngle = 0;
}
}

}
效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: