Android布局文件.xml中的自定义属性(结合封装的自定义View)

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity2"
            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.xmlset.MainActivity2

package com.cctvjiatao.xmlset;

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

public class MainActivity2 extends Activity {

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

activity_main2.xml

<!--
 xmlns:jiatao="http://schemas.android.com/apk/res/com.cctvjiatao.xmlset"
 xmlns:[自定义命名] = "http://schemas.android.com/apk/res/[本xml所属activity的包名]"
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:jiatao="http://schemas.android.com/apk/res/com.cctvjiatao.xmlset"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cctvjiatao.xmlset.MainActivity2" >

    <!-- 一定要写正确自定义View的“包名+类名”,不然汇报错:Binary XML file line #9: Error inflating class.. -->

    <com.cctvjiatao.xmlset.TestView2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        jiatao:isScroll="true"
        jiatao:linesNum="10" >
    </com.cctvjiatao.xmlset.TestView2>

</RelativeLayout>

com.cctvjiatao.xmlset.BaseView

package com.cctvjiatao.xmlset;

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

/**
 * @作者: jiatao
 * @修改时间:2016-3-12 下午9:12:42 
 * @包名:com.cctvjiatao.xmlset
 * @文件名:BaseView.java
 * @版权声明:www.cctvjiatao.com
 * @功能: 自定义View的封装类
 */
public abstract class BaseView extends View {
	
	private DrawThread 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 void onDraw(Canvas canvas) {
	    if(thread == null){
	    	thread = new DrawThread();
	    	thread.start();
	    }else{
	    	drawSub(canvas);
	    }
	}
	
	@Override
	protected void onDetachedFromWindow() {
	    super.onDetachedFromWindow();
	    isRunning = false;
	}
	
	class DrawThread extends Thread{
		@Override
		public void run() {
			while(isRunning){
				drawLogic();
				postInvalidate();
				try {
	                Thread.sleep(time);
                } catch (InterruptedException e) {
	                e.printStackTrace();
                }
			}
		}
	}

}

com.cctvjiatao.xmlset.TestView2

package com.cctvjiatao.xmlset;

import com.cctvjiatao.xmlset.R;
import com.cctvjiatao.xmlset.R.styleable;

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

/**
 * @作者: jiatao
 * @修改时间:2016-3-12 下午9:24:29 
 * @包名:com.cctvjiatao.xmlset
 * @文件名:TestView.java
 * @版权声明:com.cctvjiatao.xmlset.test
 * @功能: 测试类,在xml中设置属性
 */
public class TestView2 extends BaseView {
	
	private int linesNum = 0;
	private boolean isScroll = false;
	private float xStart = 0;
	private Paint paint = new Paint();

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

	public TestView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JiataoNumText);
		linesNum = ta.getInt(R.styleable.JiataoNumText_linesNum, 1);
		isScroll = ta.getBoolean(R.styleable.JiataoNumText_isScroll, false);
		ta.recycle();
	}

	@Override
	protected void drawSub(Canvas canvas) {
		for(int i=0; i<linesNum; i++){
			int textSize = 30+i;
			paint.setTextSize(textSize);
			canvas.drawText("cctvjiatao", xStart, textSize*(1+i), paint);
		}
	}

	@Override
	protected void drawLogic() {
		if(isScroll){
			xStart += 3;
			if(xStart > getWidth()){
				xStart = 0 - paint.measureText("cctvjiatao");
			}
		}else{
			return;
		}
	}

}
res/values/attrs.xml(新建的xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="JiataoNumText">
        <attr name="linesNum" format="integer"/>
        <attr name="isScroll" format="boolean" />
    </declare-styleable>
</resources>

运行结果:

Android布局文件.xml中的自定义属性(结合封装的自定义View)_第1张图片


你可能感兴趣的:(Android布局文件.xml中的自定义属性(结合封装的自定义View))