把任意Drawable转换成基于progress填充的drawable

来源:互联网 发布:union软件安卓 编辑:程序博客网 时间:2024/04/19 23:09
把任意Drawable转换成基于progress填充的drawable。 progress可以是四个方向:from_left, from_top, from_right, from_bottom。



FillDrawable的背后是PorterDuffColorFilter

// obtain a Drawable objectfinal Drawable drawable = getDrawable(...); // initialize boundsdrawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); // create a FillDrawable// Note, that you should call `mutate()`, otherwise changes made by FillDrawable// will be reflected in other places where this drawable is used.// It has to do it Android drawable cachefinal FillDrawable fillDrawable = new FillDrawable(FillDrawable.FROM_LEFT, drawable.mutate())    .setNormalColor(int color)    .setFillColor(int color); ... fillDrawable.setFillPercent(float percent);


此外,还提供了一个FillImageView控件来通过XML属性初始化FillDrawable:
app:fiv_normalColor="@color/normal_color"app:fiv_fillColor="@color/fill_color"app:fiv_drawable="@drawable/ic_launcher"app:fiv_from="left" <!-- (left|top|right"bottom) -->app:fiv_percent="50.0"


完整用法:
import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Random;import ru.noties.filldrawable.FillDrawable;import ru.noties.filldrawable.FillImageView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final List<FillDrawable> drawables = createDrawables();        final List<ImageView> imageViews = Arrays.asList(                (ImageView) findViewById(R.id.image_1),                (ImageView) findViewById(R.id.image_2)        );        for (int i = 0, length = Math.min(drawables.size(), imageViews.size()); i < length; i++) {            imageViews.get(i).setImageDrawable(drawables.get(i));        }        final FillImageView fillImageView1 = (FillImageView) findViewById(R.id.fill_image_view_1);        final FillImageView fillImageView2 = (FillImageView) findViewById(R.id.fill_image_view_2);        drawables.add(0, fillImageView2.getFillDrawable());        drawables.add(0, fillImageView1.getFillDrawable());        final Handler handler = new Handler();        handler.post(new FakeProgress(handler, new FakeProgress.OnProgressChange() {            @Override            public void onProgressChange(float progress) {                for (FillDrawable drawable: drawables) {                    drawable.setFillPercent(progress);                }            }        }));    }    private List<FillDrawable> createDrawables() {        final Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_android);        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());        final int alpha = 125;        final int rightColor = ContextCompat.getColor(this, R.color.color_3);        final FillDrawable right = new FillDrawable(FillDrawable.FROM_RIGHT, drawable.mutate())                .setNormalColor(ColorUtils.applyAlpha(rightColor, alpha))                .setFillColor(rightColor);        final int bottomColor = ContextCompat.getColor(this, R.color.color_4);        final FillDrawable bottom = new FillDrawable(FillDrawable.FROM_BOTTOM, drawable.mutate())                .setNormalColor(ColorUtils.applyAlpha(bottomColor, alpha))                .setFillColor(bottomColor);        return new ArrayList<FillDrawable>() {{            add(right);            add(bottom);        }};    }    private static class FakeProgress implements Runnable {        interface OnProgressChange {            void onProgressChange(float progress);        }        private final Handler mHandler;        private final Random mRandom;        private final OnProgressChange mOnProgressChange;        private final float mPercent = 1.F / 100;        int runs;        boolean isGrowing = true;        FakeProgress(Handler handler, OnProgressChange onProgressChange) {            mHandler = handler;            mOnProgressChange = onProgressChange;            mRandom = new Random();        }        @Override        public void run() {            if (isGrowing && ++runs >= 100) {                isGrowing = false;                runs = 100;            } else if (!isGrowing && --runs <= 0) {                isGrowing = true;                runs = 0;            }            final int step = mRandom.nextInt(50) + 50;            mOnProgressChange.onProgressChange(mPercent * runs);            mHandler.postDelayed(this, step);        }    }}



逐步加图片的Android控件:BlurImageView
http://www.open-open.com/lib/view/open1447423790600.html
  • 大小: 537.8 KB
  • FillDrawable-master.rar (5.6 MB)
  • 下载次数: 6
  • 查看图片附件
0 0
原创粉丝点击