80,361
社区成员
发帖
与我相关
我的任务
分享
package com.min.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Button;
public class MyButton extends Button {
private int mLeft = 0;
private int mTop = 0;
private int mRight = 50;
private int mBottom = 30;
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, int left, int top, int right, int bottom) {
super(context);
this.mLeft = left;
this.mTop = top;
this.mRight = right;
this.mBottom = bottom;
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setAlpha(128);
canvas.drawRect(new RectF(mLeft, mTop, mRight, mBottom), mPaint);
Log.i("MyButton", "======================================");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
public int getMLeft() {
return mLeft;
}
public void setMLeft(int left) {
mLeft = left;
}
public int getMTop() {
return mTop;
}
public void setMTop(int top) {
mTop = top;
}
public int getMRight() {
return mRight;
}
public void setMRight(int right) {
mRight = right;
}
public int getMBottom() {
return mBottom;
}
public void setMBottom(int bottom) {
mBottom = bottom;
}
}
public class AndroidTest extends Activity {
private final String TAG = "AndroidTest";
private Context context;
private MyButton mMyBtnSayHello, mMyBtnClickMe;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.main);
// GameSurfaceView gameSurfaceView = new GameSurfaceView(this);
// gameSurfaceView.Draw();
mMyBtnSayHello = (MyButton)findViewById(R.id.btn_say_hello);
mMyBtnClickMe = (MyButton)findViewById(R.id.btn_click_me);
int right = mMyBtnSayHello.getMLeft() + mMyBtnSayHello.getWidth();
int height = mMyBtnSayHello.getMTop() + mMyBtnSayHello.getHeight();
mMyBtnSayHello.setMRight(right);
mMyBtnSayHello.setMBottom(height);
mMyBtnSayHello.invalidate();
mMyBtnClickMe.setMRight(mMyBtnClickMe.getMLeft() + mMyBtnClickMe.getWidth());
mMyBtnClickMe.setMBottom(mMyBtnClickMe.getMTop() + mMyBtnClickMe.getHeight());
mMyBtnClickMe.invalidate();
Log.i(TAG, "onCreate end...");
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.min.test.MyButton
android:id="@+id/btn_say_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hello"
/>
<com.min.test.MyButton
android:id="@+id/btn_click_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
/>
</LinearLayout>
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Log.i("MyButton", "==== onDraw(Canvas) ====");
int left, top, right, bottom;
left = MARGIN;
top = MARGIN;
right = getWidth() - MARGIN;
bottom = getHeight() - MARGIN;
Paint mPaint = new Paint();
if (this.hasFocus() || this.isPressed()) {
mPaint.setColor(Color.BLUE);
Log.i("MyButton", "Set color to BLUE.");
} else {
mPaint.setColor(Color.GRAY);
Log.i("MyButton", "Set color to GRAY.");
}
Log.i(TAG, "width = " + getWidth() + ", height = " + getHeight());
// mPaint.setAlpha(128);
canvas.drawRect(new RectF(left, top, right, bottom), mPaint);
// set text color
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(16);
// FontMetrics对象
FontMetrics fontMetrics = mPaint.getFontMetrics();
// draw text
float x = (getWidth() - mPaint.measureText(getText().toString())) / 2;
float y = (getHeight() - mPaint.getTextSize()) / 2;
Log.i(TAG, "mPaint.getTextSize() = " + mPaint.getTextSize() + ", getHeight() = " + getHeight());
Log.i(TAG, "x = " + x + ", y = " + y);
canvas.drawText(getText().toString(), x, y, mPaint);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setAlpha(128);
canvas.drawRect(new RectF(0, 0, getWidth(), getHeight()), mPaint);
Log.i("MyButton", "==== onDraw(Canvas) ====");
Log.i(TAG, "width = " + getWidth() + ", height = " + getHeight());
}
<!-- 定义my_style_button,用android的Button作为parent -->
<style name="my_style_button" parent="@android:style/Widget.Button">
<!-- 定义与指定View相关的若干属性 -->
<item name="android:hint">"load from style my button"</item>
<item name="android:textColorHint">#ff1200</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FF00FF</item>
<item name="android:textColorHighlight">#cccccc</item>
<item name="android:typeface">monospace</item>
<item name="android:background">@drawable/button_normal</item>
</style>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/my_style_button"
android:text="Test Only"
/>