首页 > 其他分享 >把任意Drawable转换成基于progress填充的drawable

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

时间:2023-05-01 16:08:32浏览次数:44  
标签:color new final FillDrawable import progress Drawable drawable


把任意Drawable转换成基于progress填充的drawable。 progress可以是四个方向:from_left, from_top, from_right, from_bottom。



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



FillDrawable的背后是PorterDuffColorFilter


// obtain a Drawable object
final Drawable drawable = getDrawable(...);
 
// initialize bounds
drawable.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 cache
final 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

标签:color,new,final,FillDrawable,import,progress,Drawable,drawable
From: https://blog.51cto.com/u_5454003/6238919

相关文章

  • LayerDrawable层叠样式layer-list
    layer-list可以将多个图片按照顺序层叠起来。语法:在drawalbe/drawable-layer.xml中<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@android:color/white"/><itemand......
  • Codeforces Round #156 (Div. 2) C. Almost Arithmetical Progression dp
    Genalovessequencesofnumbers.Recently,hehasdiscoveredanewtypeofsequenceswhichhecalledanalmostarithmeticalprogression.Asequenceisanalmostarithmeticalprogression,ifitselementscanberepresentedas:a1 = p,wherepissomeintege......
  • nprogress 使用
    #https://www.npmjs.com/package/nprogresspnpminprogress//main.jsimport'nprogress/nprogress.css'//App.vue自定义一下loading的滚动条样式<style>#nprogress.bar{background:#f4f4f4!important;height:3px!important;}</style>/......
  • 关于Drawable转Bitmap的错误,为0的处理办法
    这个问题困惑我许久,主要是在于给的报错信息误导了我,报错信息如下:后来,我跟飞哥探讨(请教)了一番,得知可能返回的不是0,于是乎我就打印了一个log,来测试一下,发现并不是0,而是-1!!!!如此,我便寻得缘由,点进去方法,看到了这样一个方法:恍然大悟!!!感谢飞哥。......
  • Android动态设置drawableRight
    DrawablerightDrawable=getResources().getDrawable(R.drawable.icon);//调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示rightDrawable.setBounds(0,0,rightDrawable.getMinimumWidth(),rightDrawable.getMinimumHeight());//left,top,r......
  • vue项目启动时 `webpack-dev-server –inline –progress –config build/webpack.dev
    vue项目在npmrundev时报错[email protected]:webpack-dev-server--inline--progress--configbuild/webpack.dev.conf.js解决这类问题主要分两种情况这个项目已经构建好的项目,你只是从git、snv或者其他地方引入,别人能运行你不能运行这是一个新构建的vue项目第一......
  • 报错ValidationError: Progress Plugin Invalid Options (非常重要)
    此方法可以解决:使用Vue-ptf时报错:maintemplate.hooks.hotbootstraphasbeenremoved(useyourownruntimemodule和package-lock.json导包( less,lessloader)和(npminstallpdfjs-dist--save)等后出现ValidationError:ProgressPluginInvalidOptions两种问......
  • vue中使用nprogress优化导航条和请求数据的时候进行加载中显示
    以下内容仅供学习使用先进行安装npminstall--savenprogress或者yarnaddnprogress在你封装的request.js中使用importNProgressfrom'nprogress'import'nprogress/nprogress.css'在axios.interceptors.request.use(config=>{},请求开始的时候使用NProgres......
  • Android Button设置android:background=“@drawable/xxx“无效,<item android:drawable
    问题描述:AndroidButton设置android:background=“@drawable/xxx“无效,<itemandroid:drawable="@color/purple_200"/>无效  解决方法:找到res/values/themes.xm......
  • 直播平台搭建源码,Android进度条ProgressBar颜色的渐变设置
    直播平台搭建源码,Android进度条ProgressBar颜色的渐变设置1.第一步 在res/drawable目录下新建自己的样式文件my_progressBar.xml文件。 <?xmlversion="1.0"encod......