android自訂控制項

來源:互聯網
上載者:User

標籤:span   listener   listen   log   resources   false   ack   pack   sources   

package com.example.app.sw.kg;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MySwitchBtnView extends View implements OnClickListener{        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);    private boolean flag = false ;    private Context mContext ;        Bitmap bitmap_open = null ;    Bitmap bitmap_close = null;    public MySwitchBtnView(Context context, AttributeSet attrs) {        super(context, attrs);        mContext = context;        init();    }    private void init() {        setOnClickListener(this);        bitmap_open = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.btn_open);        bitmap_close = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.btn_close);            }        @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        setMeasuredDimension(bitmap_open.getWidth(), bitmap_close.getHeight());    }        public MySwitchBtnView(Context context) {        super(context);        mContext = context ;        init();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if(flag){            //繪製開            canvas.drawBitmap(bitmap_open, 0, 0,paint);            Toast.makeText(mContext, "被開啟了", 0).show();        }else{            //繪製關            canvas.drawBitmap(bitmap_close, 0, 0,paint);            Toast.makeText(mContext, "被關閉了", 0).show();        }    }    @Override    public void onClick(View v) {        if(flag){            flag = false ;        }else{            flag = true ;        }        invalidate(); //重新整理螢幕    }}

 

android自訂控制項

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.