但使书种多,会有岁稔时。这篇文章主要讲述android自定义控件相关的知识,希望能为你提供帮助。
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自定义控件】
推荐阅读
- android viewpager嵌套使用photoview异常问题
- Django实现自定义template页面并在admin site的app模块中加入自定义跳转链接
- Android实践之ScrollView中滑动冲突处理
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request&q
- Android实现搜索框内自动完成文本框
- Android(ListView自动生成列表条目)
- Android:普通对话框Dialog
- 07_android入门_採用HttpClient的POST方式GET方式分别实现登陆案例
- Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connect