:golf:自定义Drawable

Posted 春招进大厂的梦想家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了:golf:自定义Drawable相关的知识,希望对你有一定的参考价值。

⛳️自定义Drawable

1、创建一个自定义类

这里是用来实现一个圆角效果,可用作任意view的背景

package com.example.dailypractice.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class RoundImageDrawable extends Drawable {

    private Paint mPaint;
    private Bitmap mBitmap;

    private RectF rectF;

    public RoundImageDrawable(Bitmap bitmap)
    {
        mBitmap = bitmap;
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom)
    {
        super.setBounds(left, top, right, bottom);
        rectF = new RectF(left, top, right, bottom);
    }

    @Override
    public void draw(Canvas canvas)
    {
        canvas.drawRoundRect(rectF, 30, 30, mPaint);
    }

    @Override
    public int getIntrinsicWidth()
    {
        return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight()
    {
        return mBitmap.getHeight();
    }

    @Override
    public void setAlpha(int alpha)
    {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf)
    {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity()
    {
        return PixelFormat.TRANSLUCENT;
    }

}

其实主要的功能函数就是onDraw(),其他函数用来获取宽高、透明度等基本参数

2、在xml布局文件下添加自己想要的标签

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".MainActivity2">

    <ImageView
        android:id="@+id/main2_img"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"/>

</LinearLayout>

3、在.java文件下进行逻辑编辑,给img设置自定义的Drawable背景

public class MainActivity2 extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //使用自定义Drawable
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.one);
        imageView = findViewById(R.id.main2_img);
        imageView.setImageDrawable(new RoundImageDrawable(bitmap));
    }
}

以上是关于:golf:自定义Drawable的主要内容,如果未能解决你的问题,请参考以下文章

Seekbar进度drawable异常行为onPause

Android自定义View之自定义drawable

Android自定义View之自定义drawable

Android自定义View之自定义drawable

text 这是一个简单的代码,用于为工具栏上的后退按钮设置自定义drawable

VSCode自定义代码片段——CSS选择器