Android中ClipDrawable的使用和自定义ProgressBar
来源:互联网 发布:韩国娱乐圈天涯知乎 编辑:程序博客网 时间:2024/05/06 02:17
简介
ClipDrawable 对一个Drawable进行剪切操作,可控制这个Drawable的剪切区域,以及相对容器的对齐方式,它根据level值决定剪切区域的大小。官方文档说明,level的范围大小为0-10000,0表示完全不显示,10000表示完全显示,通过ClipDrawable提供的setLevel(int level)方法来设置剪切区域。
XML配置
ClipDrawable可通过XML进行配置
<!--clip 定义这是个ClipDrawable,必须作为根元素android:drawable 表示该ClipDrawable引用的drawable资源(必须)android:clipOrientation 裁剪的方向android:gravity 指定从哪个位置裁剪(多个值之间用“|”分隔)top:从下向上剪切bottom:从上向下剪切。。。。--><?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="flag"android:clipOrientation=["horizontal" | "vertical"]android:drawable="reference"></clip>
简单应用
用ClipDrawable的xml配置,简单编写一个自定义的Progress。效果图
源码
控件的布局文件yx_clipprogress_layout.xml<?xml version="1.0" encoding="utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iv_progress" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:background="@drawable/yx_clipprogress_bg" android:scaleType="centerInside" android:src="@drawable/yx_clipprogress_clip_xml" ></ImageView>资源文件yx_clipprogress_clip_xml.xml<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="vertical" android:gravity="bottom" android:drawable="@drawable/yx_clipprogress_loading" ></clip>属性配置文件 attrs.xml<declare-styleable name="YXClipProgress"> <attr name="autoRuning" format="boolean"/> <attr name="progress" format="integer"/> </declare-styleable>自定义控件类package com.example.ztest2;import java.lang.ref.WeakReference;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.ClipDrawable;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.FrameLayout;import android.widget.ImageView;public class YXClipProgress extends FrameLayout{/** * ClipDrawable-level的最大值,范围在0-10000之间,0 不显示,10000充分显示 * 可以类比progress 即progress = MAX_PROGRESS/100; */private static final int MAX_PROGRESS = 10000;/* 是否自动增加进度 */private boolean auto_runing;private static final int MESSAGE_WHAT = 0x123456;private ClipDrawable mClipDrawable;private int mProgress;private PHandler handler = null;/** * 设置进度值 * @param progress进度值0-100 */public void setYXProgress(int progress){auto_runing = false;handler.sendEmptyMessage(MESSAGE_WHAT);this.mProgress = (progress * 100);}/** * stop */public void stop(){mProgress = 0;auto_runing = false;}public YXClipProgress(Context context) {this(context,null);// TODO Auto-generated constructor stub}public YXClipProgress(Context context, AttributeSet attrs) {this(context, attrs,0);// TODO Auto-generated constructor stub}public YXClipProgress(Context context, AttributeSet set, int defStyle) {super(context, set, defStyle);// TODO Auto-generated constructor stubcustomInitAttribute(context,set,0);initThread(context);}private void initThread(Context context) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {while(auto_runing){handler.sendEmptyMessage(MESSAGE_WHAT);if(mProgress>MAX_PROGRESS){mProgress = 0;}mProgress += 100;try {Thread.sleep(50);} catch (Exception e) {e.printStackTrace();}}}}).start();}private void customInitAttribute(Context context, AttributeSet set, int defStyle) {/*开启Handler*/handler = new PHandler(this);/* 配置属性值 */TypedArray a = context.obtainStyledAttributes(set,R.styleable.YXClipProgress);auto_runing = a.getBoolean(R.styleable.YXClipProgress_autoRuning, false);mProgress = a.getInt(R.styleable.YXClipProgress_progress, 0) * 100;a.recycle();/* inflate */View view = LayoutInflater.from(context).inflate(R.layout.yx_clipprogress_layout, null);addView(view);ImageView imageView = (ImageView) findViewById(R.id.iv_progress);mClipDrawable = (ClipDrawable) imageView.getDrawable();mClipDrawable.setLevel(mProgress);}private static class PHandler extends Handler{WeakReference<YXClipProgress> mp;PHandler(YXClipProgress a){mp = new WeakReference<YXClipProgress>(a);}@Overridepublic void handleMessage(Message msg) {YXClipProgress t = mp.get();if(msg.what == MESSAGE_WHAT){t.mClipDrawable.setLevel(t.mProgress);}}}}此控件类关键在于处理消息部分中的setLevel。附件图片
yx_clipprogress_bg.pngyx_clipprogress_loading.png
0 0
- Android中ClipDrawable的使用和自定义ProgressBar
- Android中ClipDrawable的用法
- android ClipDrawable的基本使用
- Android中图片的裁剪和徐徐展开控件ClipDrawable
- Android中自定义ProgressBar
- android中ProgressBar的使用
- android中ProgressBar的使用
- Android 中 progressBar的使用
- Android中自定义ProgressBar的样式
- Android中自定义水平的ProgressBar
- Android 使用自定义属性的ProgressBar
- Android自定义水平和圆形的progressBar
- ProgressBar的自定义使用和详细说明
- 自定义listview和ProgressBar的简单使用
- Android中关于ClipDrawable
- ClipDrawable资源的使用
- ClipDrawable的使用
- clipdrawable的使用
- 邮箱大师与微软Acompli功能评测
- Android中本地存储方式之一:Sharedperferences
- MouseMotionListener和MouseListener
- 内核是如何管理内存的?
- 黑马程序员------反射机制
- Android中ClipDrawable的使用和自定义ProgressBar
- 思科CCNA第三学期第五章答案
- ajax传输数据的业务实现
- 三张表联合查询
- android自定义view的案例。
- 彩信拦截
- 开源项目PullToRefresh详解
- Linux操作命令(三)
- jsp探针