You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Android:自定义View中在Canvas上绘制VectorDrawable XML的问题

嘿,我来帮你搞定在自定义View里绘制VectorDrawable的问题!其实步骤很简单,我给你拆解一下:

步骤1:获取VectorDrawable实例

首先要拿到你的starttask.xml对应的Drawable对象,这里推荐用AppCompatResources.getDrawable(),它能完美兼容Android各个版本,不用操心API等级的问题:

Drawable vectorDrawable = AppCompatResources.getDrawable(context, R.drawable.starttask);

步骤2:设置Drawable的显示边界

VectorDrawable默认是没有大小的,所以必须给它设置一个显示区域(bounds)。你可以在onSizeChanged方法里设置,这样当View大小变化时,Drawable也能跟着适配:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // 让Drawable填满整个View,你也可以自定义宽高和位置
    vectorDrawable.setBounds(0, 0, w, h);
}

步骤3:在onDraw里绘制

最后在onDraw方法里调用Drawable的draw()方法,把Canvas传进去就搞定了:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    vectorDrawable.draw(canvas);
}

额外实用技巧

  • 修改颜色:如果想给VectorDrawable换个颜色,不用改xml,直接用setTint()
    vectorDrawable.setTint(ContextCompat.getColor(context, R.color.your_target_color));
    
  • 自定义位置和大小:如果不想让Drawable填满整个View,直接调整setBounds()的参数就行,比如想在(100,100)的位置绘制一个200x200的图标:
    vectorDrawable.setBounds(100, 100, 300, 300); // 左、上、右、下坐标
    
  • 缩放/平移:如果需要对Drawable做变换,可以用Canvas的save/restore机制:
    canvas.save();
    canvas.scale(0.8f, 0.8f); // 缩小到80%
    vectorDrawable.draw(canvas);
    canvas.restore();
    

完整示例代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;

public class TaskStartView extends View {
    private Drawable mStartTaskDrawable;

    public TaskStartView(Context context) {
        super(context);
        init(context);
    }

    public TaskStartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TaskStartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        // 获取VectorDrawable实例
        mStartTaskDrawable = AppCompatResources.getDrawable(context, R.drawable.starttask);
        // 可选:设置自定义色调
        mStartTaskDrawable.setTint(ContextCompat.getColor(context, R.color.blue_500));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 设置Drawable的显示边界
        mStartTaskDrawable.setBounds(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制VectorDrawable
        mStartTaskDrawable.draw(canvas);
    }
}

注意事项

确保你的项目依赖了AndroidX AppCompat库,在Module级别build.gradle里添加:

implementation 'androidx.appcompat:appcompat:1.6.1'

(版本号可以用最新的,比如现在的1.6.1,或者更高版本)

这样就能在你的自定义View里完美绘制那个Vector图标啦!

内容的提问来源于stack exchange,提问作者Nisarg Jani

火山引擎 最新活动