您当前的位置: 首页 >  android

xiangzhihong8

暂无认证

  • 4浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android自定义button点击效果

xiangzhihong8 发布时间:2013-12-24 10:18:39 ,浏览量:4

  1. package com.min.androidtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Rect;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.widget.Button;  
  14. import android.widget.LinearLayout;  
  15.   
  16. public class AndroidTest extends Activity {  
  17.     private static final String TAG = "AndroidTest";  
  18.   
  19.     private static LinearLayout ll;  
  20.     private Button mBtn;  
  21.     private MyView mMyView;  
  22.     private boolean mbClickFlg = false;  
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27. //        setContentView(R.layout.main);  
  28.           
  29.         // 设置画面布局  
  30.         ll = new LinearLayout(this.getBaseContext());  
  31.         ll.setOrientation(LinearLayout.VERTICAL);  
  32.   
  33.         // button  
  34.         mBtn = new Button(this.getBaseContext());  
  35.         mBtn.setText("Draw");  
  36.         //   
  37.         mBtn.setTextColor(0xff000000);  
  38.         mBtn.setTextSize(16);  
  39.         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(  
  40.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  41.   
  42.         lp.topMargin = 5;  
  43.         lp.leftMargin = 5;  
  44.         ll.addView(mBtn, lp);  
  45.   
  46.         // 设置画图view  
  47.         LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(  
  48.                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  49.         lp1.weight = 1;  
  50.         Rect rect = new Rect(10, 40, 90, 66);  
  51.         mMyView = new MyView(this.getBaseContext(), rect);  
  52.         mMyView.setOnClickListener(new View.OnClickListener() {  
  53.   
  54.             public void onClick(View v) {  
  55.                 // TODO Auto-generated method stub  
  56.                 Log.d(TAG, "mMyView clicked.");  
  57.                 if (mbClickFlg) {  
  58.                     mMyView.setStrText("Click Me");  
  59.                 } else {  
  60.                     mMyView.setStrText("Again...");  
  61.                 }  
  62.                 mbClickFlg = !mbClickFlg;  
  63.                 mMyView.postInvalidate();  
  64.             }  
  65.               
  66.         });  
  67.           
  68.         ll.addView(mMyView, lp1);  
  69.         setContentView(ll);  
  70.   
  71.         // 设置监听按钮点击事件  
  72.         mBtn.setOnClickListener(new View.OnClickListener() {  
  73.   
  74.             public void onClick(View v) {  
  75.                 // TODO Auto-generated method stub  
  76.                 mMyView.drawLine();  
  77.                 mMyView.postInvalidate();  
  78.             }  
  79.               
  80.         });  
  81.   
  82.     }  
  83.   
  84.     // 自定义画图类,继承自View类  
  85.     public class MyView extends View {     
  86.         private Paint paint;  
  87.         private boolean bDrawLineFlg = false;  
  88.         private Rect mRect;  
  89.         private String mStrText = "having";  
  90.           
  91.         MyView(Context context) {     
  92.             super(context);     
  93.             // 生成paint   
  94.             paint = new Paint();    
  95.         }  
  96.           
  97.         MyView(Context context, Rect rect) {     
  98.             super(context);  
  99.             // 生成paint   
  100.             paint = new Paint();  
  101.             mRect = rect;  
  102.         }  
  103.              
  104.         @Override    
  105.         protected void onDraw(Canvas canvas) {     
  106.             // TODO Auto-generated method stub     
  107.             super.onDraw(canvas);     
  108.               
  109.             // 填充整个画布  
  110.             canvas.drawColor(Color.GRAY);  
  111. //            canvas.drawRGB(255, 255, 255); // 也可以  
  112.   
  113.               
  114.               
  115.             // 画图  
  116.            if (bDrawLineFlg) {  
  117.                 Log.d(TAG, "drawing");  
  118.                   
  119.                 // Draw top 6 lines  
  120.                 paint.setColor(0xFFFFC0CB);    
  121.                 canvas.drawLine(mRect.left + 2, mRect.top, mRect.right - 2, mRect.top, paint);  
  122.                 canvas.drawLine(mRect.left + 1, mRect.top + 1, mRect.right -1, mRect.top + 1, paint);  
  123.                 for (int i = 2; i 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.1727s