自定义View实例——下雨天画面(通过布局文件.xml中的自定义属性来设值)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cctvjiatao.rainview"
    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.rainview.MainActivity2

package com.cctvjiatao.rainview;

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

<pre name="code" class="html"><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.rainview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cctvjiatao.rainview.MainActivity" >

    <com.cctvjiatao.rainview.v3.ManyRain
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff000000"
        jiatao:rainNum = "100"
        jiatao:size = "100"
        jiatao:rainColor = "0xffffffff"
        jiatao:randColor = "false"
        />

</RelativeLayout>
 

res/value/attrs.xml(新建的资源文件)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RainView">
        <attr name="rainNum" format="integer"/>
        <attr name="size" format="integer"/>
        <attr name="rainColor" format="integer"/>
        <attr name="randColor" format="boolean"/>
    </declare-styleable>
    
</resources>

com.cctvjiatao.rainview.v2.BaseView

<pre name="code" class="java">package com.cctvjiatao.rainview.v2;

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

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

	private DrawThread thread;
	private boolean isRunning = true;
	private long sleepTime = 30;

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

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

	protected abstract void drawSub(Canvas canvas);

	protected abstract void drawLogic();

	protected abstract void init();

	@Override
	protected final 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() {
			init();
			while (isRunning) {
				drawLogic();
				postInvalidate();
				try {
					Thread.sleep(sleepTime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
 

com.cctvjiatao.rainview.v2.RainItem

</pre><pre name="code" class="java">package com.cctvjiatao.rainview.v2;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Paint;
/**
 * @作者: jiatao
 * @修改时间:2016-3-13 下午12:33:39
 * @包名:com.cctvjiatao.rainview.v2
 * @文件名:RainItem.java
 * @版权声明:www.cctvjiatao.com
 * @功能: 雨滴的封装类
 */
public class RainItem {

	private int width, height;
	private float startX, startY, stopX, stopY, sizeX, sizeY;
	private float speed;// 可控制速度
	private Random random;
	private Paint paint = new Paint();

	private int size = 20;
	private int color ;
	private boolean randColor = false;;

	public RainItem(int width, int height) {
		this.width = width;
		this.height = height;
		init();
	}

	public RainItem(int width, int height, int size) {
		this.size = size;
		this.width = width;
		this.height = height;
		init();
	}

	public RainItem(int width, int height, int size, int color) {
		this.color = color;
		this.size = size;
		this.width = width;
		this.height = height;
		init();
	}

	public RainItem(int width, int height, int size, int color, boolean randColor) {
		this.randColor = randColor;
		this.color = color;
		this.size = size;
		this.width = width;
		this.height = height;
		init();
	}
	
	public RainItem(int width, int height, int size, int color, boolean randColor, float speed) {
		this.randColor = randColor;
		this.color = color;
		this.size = size;
		this.speed = speed;
		this.width = width;
		this.height = height;
		init();
	}

	private void init() {
		random = new Random();
		sizeX = 1 + random.nextInt(size / 2);
		sizeY = 10 + random.nextInt(size);
		startX = random.nextInt(width);
		startY = random.nextInt(height);
		stopX = startX + sizeX;
		stopY = startY + sizeY;
		speed = 0.2f + random.nextFloat();
		paint = new Paint();
		if (randColor) {
			int r = random.nextInt(256);
			int g = random.nextInt(256);
			int b = random.nextInt(256);

			paint.setARGB(255, r, g, b);
		} else {
			paint.setColor(color);
		}
	}

	public void draw(Canvas canvas) {
		canvas.drawLine(startX, startY, stopX, stopY, paint);
	}

	public void move() {
		startX += sizeX * speed;
		stopX += sizeX * speed;
		startY += sizeY * speed;
		stopY += sizeY * speed;
		if (startY > height) {
			init();
		}
	}
}
com.cctvjiatao.rainview.v3.ManyRain
<pre name="code" class="java">package com.cctvjiatao.rainview.v3;

import java.util.ArrayList;

import com.cctvjiatao.rainview.R;
import com.cctvjiatao.rainview.v2.BaseView;
import com.cctvjiatao.rainview.v2.RainItem;

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

/**
 * @作者: jiatao
 * @修改时间:2016-3-13 下午14:33:39
 * @包名:com.cctvjiatao.rainview.v2
 * @文件名:ManyRain.java
 * @版权声明:www.cctvjiatao.com
 * @功能: 下雨的执行类,通过布局文件.xml中的自定义属性来设值
 */
public class ManyRain extends BaseView {

	private ArrayList<RainItem> list = new ArrayList<RainItem>();
	private int rainNum;// 雨滴的数量
	private int size;// 移动的距离
	private int rainColor;// 画笔的初始颜色
	private boolean randColor; // 是否变色

	public ManyRain(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RainView);
		rainNum = ta.getInteger(R.styleable.RainView_rainNum, 80);
		size = ta.getInteger(R.styleable.RainView_size, 20);
		rainColor = ta.getInteger(R.styleable.RainView_rainColor, 0xffffffff);
		randColor = ta.getBoolean(R.styleable.RainView_randColor, false);
		ta.recycle();
	}

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

	@Override
	protected void drawSub(Canvas canvas) {

		for (RainItem item : list) {
			item.draw(canvas);
		}
	}

	@Override
	protected void drawLogic() {
		for (RainItem item : list) {
			item.move();
		}
	}

	@Override
	protected void init() {
		for (int i = 0; i < rainNum; i++) {
			RainItem item = new RainItem(getWidth(), getHeight(), size, rainColor, randColor);
			list.add(item);
		}
	}

}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值