Android图片处理的一些技巧

Palette

使用Palette的 API,能够让我们从Bitmap中获取对应的色调,修改当前的主题色调。
需要添加依赖implementation 'androidx.palette:palette:1.0.0’

//创建Palette对象
        Palette.from(getImageBit(findViewById(R.id.palette_iv))).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(@Nullable Palette palette) {
                if (palette!=null){
                    Window window=getWindow();
                    //设置色调
                    window.setStatusBarColor(palette.getDarkMutedColor(Color.RED));
                }
            }
        });
private Bitmap getImageBit(ImageView view){
        return ((BitmapDrawable)(view.getDrawable())).getBitmap();
    }

在这里插入图片描述

视图与阴影

View的属性:X,Y,Z
Z= clevation +translationZ

android:elevation="20sp"

在这里插入图片描述

Button ev_tv=findViewById(R.id.ev_tv);
        ev_tv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        ev_tv.animate().translationZ(20);
                        break;
                    case MotionEvent.ACTION_UP:
                        ev_tv.animate().translationZ(0);
                        break;
                }
                return false;
            }
        });
Tinting(着色)

通过设置不同的tint与tintMode来实现不同的效果
在这里插入图片描述

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_gravity="center"
            android:elevation="5dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:layout_gravity="center"
            android:elevation="5dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tint="@android:color/holo_blue_bright" />
        <ImageView
            android:layout_gravity="center"
            android:elevation="5dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tint="@android:color/holo_blue_bright"
            android:tintMode="add"/>
        <ImageView
            android:layout_gravity="center"
            android:elevation="5dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tint="@android:color/holo_blue_bright"
            android:tintMode="multiply"/>
    </LinearLayout>
Clipping(裁剪)

Clipping可以让我们改变一个视图的外形。要使用Clipping ,首先需要使用ViewOutlineProvider来修改outline,然后再通过setOutlineProvider将outline 作用给视图。
在这里插入图片描述

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:background="@color/colorPrimary"
            android:id="@+id/tv_rect"
            android:layout_gravity="center"
            android:layout_marginTop="20sp"
            android:elevation="1dp"
            android:layout_width="100dp"
            android:layout_height="100dp"/>
        <TextView
            android:background="@color/colorAccent"
            android:id="@+id/tv_circle"
            android:layout_gravity="center"
            android:layout_marginTop="20sp"
            android:elevation="1dp"
            android:layout_width="100dp"
            android:layout_height="100dp"/>
    </LinearLayout>
TextView tv_rect=findViewById(R.id.tv_rect);
        TextView tv_circle=findViewById(R.id.tv_circle);
        ViewOutlineProvider rect=new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                //修改outline为指定形状
                outline.setRoundRect(0,0,view.getHeight(),view.getWidth(),30);
            }
        };
        ViewOutlineProvider circle=new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0,0,view.getWidth(),view.getHeight());
            }
        };
        tv_circle.setOutlineProvider(circle);
        tv_rect.setOutlineProvider(rect);
        tv_rect.setClipToOutline(true);//开启裁剪
        tv_circle.setClipToOutline(true);//开启裁剪
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值