android自定义button点击效果
xiangzhihong8 发布时间:2013-12-24 10:18:39 ,浏览量:4
- package com.min.androidtest;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.LinearLayout;
-
- public class AndroidTest extends Activity {
- private static final String TAG = "AndroidTest";
-
- private static LinearLayout ll;
- private Button mBtn;
- private MyView mMyView;
- private boolean mbClickFlg = false;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
-
- // 设置画面布局
- ll = new LinearLayout(this.getBaseContext());
- ll.setOrientation(LinearLayout.VERTICAL);
-
- // button
- mBtn = new Button(this.getBaseContext());
- mBtn.setText("Draw");
- //
- mBtn.setTextColor(0xff000000);
- mBtn.setTextSize(16);
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
-
- lp.topMargin = 5;
- lp.leftMargin = 5;
- ll.addView(mBtn, lp);
-
- // 设置画图view
- LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- lp1.weight = 1;
- Rect rect = new Rect(10, 40, 90, 66);
- mMyView = new MyView(this.getBaseContext(), rect);
- mMyView.setOnClickListener(new View.OnClickListener() {
-
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Log.d(TAG, "mMyView clicked.");
- if (mbClickFlg) {
- mMyView.setStrText("Click Me");
- } else {
- mMyView.setStrText("Again...");
- }
- mbClickFlg = !mbClickFlg;
- mMyView.postInvalidate();
- }
-
- });
-
- ll.addView(mMyView, lp1);
- setContentView(ll);
-
- // 设置监听按钮点击事件
- mBtn.setOnClickListener(new View.OnClickListener() {
-
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mMyView.drawLine();
- mMyView.postInvalidate();
- }
-
- });
-
- }
-
- // 自定义画图类,继承自View类
- public class MyView extends View {
- private Paint paint;
- private boolean bDrawLineFlg = false;
- private Rect mRect;
- private String mStrText = "having";
-
- MyView(Context context) {
- super(context);
- // 生成paint
- paint = new Paint();
- }
-
- MyView(Context context, Rect rect) {
- super(context);
- // 生成paint
- paint = new Paint();
- mRect = rect;
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
-
- // 填充整个画布
- canvas.drawColor(Color.GRAY);
- // canvas.drawRGB(255, 255, 255); // 也可以
-
-
-
- // 画图
- if (bDrawLineFlg) {
- Log.d(TAG, "drawing");
-
- // Draw top 6 lines
- paint.setColor(0xFFFFC0CB);
- canvas.drawLine(mRect.left + 2, mRect.top, mRect.right - 2, mRect.top, paint);
- canvas.drawLine(mRect.left + 1, mRect.top + 1, mRect.right -1, mRect.top + 1, paint);
- for (int i = 2; i
1482932726
查看更多评论