Androidステップカスタムコントロール1


オリジナルコントロールを拡張し、新しい機能を追加
一般的にonDraw()メソッドでは、オリジナルコントロールを拡張します.
次に、TextViewを例に、オリジナルコントロールを拡張する方法を使用して新しいコントロールを作成する方法を見てみましょう.
/*
 *          
 * */

public class M_TextView extends TextView{



	public M_TextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	public M_TextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public M_TextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Paint mPaint1 = new Paint();
		mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
		mPaint1.setStyle(Paint.Style.FILL);
		Paint mPaint2 = new Paint();
		mPaint2.setColor(Color.YELLOW);
		mPaint2.setStyle(Paint.Style.FILL);
		//      
		canvas.drawRect(0, 0, getMeasuredWidth(),getMeasuredHeight(), mPaint1);
		//      
		canvas.drawRect(10, 10, getMeasuredWidth()-10, getMeasuredHeight()-10, mPaint2);
		canvas.save();
		//       10  
		canvas.translate(10, 0);
		super.onDraw(canvas);
		canvas.restore();
	}

}
は、レイアウトファイルで使用されます.
 <com.example.kongjian_1.M_TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="android"
        android:textColor="@android:color/black"
        android:textSize="25sp" >
    </com.example.kongjian_1.M_TextView>

例2:テキストにフラッシュ効果を追加するには:
public class M_TextView2  extends TextView {

	public M_TextView2(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public M_TextView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public M_TextView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	private LinearGradient mLinearGradient;
	private Matrix mGradientMatrix;
	private Paint mPaint;
	private int mViewWidth = 0;
	private int mTranslate = 0;


	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (mViewWidth == 0) {
			mViewWidth = getMeasuredWidth();
			if (mViewWidth > 0) {
				mPaint = getPaint();
				mLinearGradient = new LinearGradient(
						0,
						0,
						mViewWidth,
						0,
						new int[]{
								Color.BLUE, 0xffffffff,
								Color.BLUE},
								null,
								Shader.TileMode.CLAMP);
				mPaint.setShader(mLinearGradient);
				mGradientMatrix = new Matrix();
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mGradientMatrix != null) {
			mTranslate += mViewWidth / 5;
			if (mTranslate > 2 * mViewWidth) {
				mTranslate = -mViewWidth;
			}
			mGradientMatrix.setTranslate(mTranslate, 0);
			mLinearGradient.setLocalMatrix(mGradientMatrix);
			postInvalidateDelayed(50);
		}
	}
}

xmlで使用:
<com.example.kongjian_1.M_TextView2
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="android"
        android:textColor="@android:color/black"
        android:textSize="25sp" >
    </com.example.kongjian_1.M_TextView2>