ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

android – 如何以编程方式使用笔画制作圆形绘图?

2019-07-24 12:23:31  阅读:304  来源: 互联网

标签:android-shapedrawable android android-drawable layerdrawable


背景

我想要一个填充圆圈,具有一定颜色和宽度的笔划,以及内部的图像.

这可以很容易地用XML完成(这只是一个示例):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <size
                android:width="120dp" android:height="120dp"/>
            <solid android:color="#ffff0000"/>
            <stroke
                android:width="3dp" android:color="#ff00ff00"/>
        </shape>
    </item>
    <item
        android:width="60dp" android:height="60dp" android:drawable="@android:drawable/btn_star"
        android:gravity="center"/>
</layer-list>

enter image description here

问题

我需要具有以上某些属性以编程方式进行更改,因此我无法使用XML文件,但这个想法仍然是相同的.

事实是,我无法找到一种简单的方法来在OvalShape drawable上绘制,就像在XML中一样.我找不到这样做的功能.

我尝试了什么

StackOverflow上有解决方案,但我找不到一个运行良好的解决方案.我发现只有一个是here,但它的划线正在削减.

但是,我通过一种方式来解决这个问题,通过使用一个仅用于笔划的XML:

stroke_drawable.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <stroke
        android:width="4dp" android:color="@android:color/white"/>
</shape>

码:

    final int strokeDrawableResId = R.drawable.stroke_drawable;
    Drawable innerDrawable = ResourcesCompat.getDrawable(getResources(), ..., null);
    final Drawable strokeDrawable = ResourcesCompat.getDrawable(getResources(), strokeDrawableResId, null);
    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    int size = ...;
    biggerCircle.setIntrinsicHeight(size);
    biggerCircle.setIntrinsicWidth(size);
    biggerCircle.getPaint().setColor(0xffff0000);
    biggerCircle.setBounds(new Rect(0, 0, size, size));
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{biggerCircle, strokeDrawable, innerDrawable});

    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(layerDrawable);

它可以工作,但它不是完全以编程方式(笔划在XML中定义).

这个问题

如何将代码更改为完全编程?

编辑:我尝试了这里建议的,而不是一个额外的drawable为背景,因为我需要一个drawable,我使用LayerDrawable:

    Drawable innerDrawable = ResourcesCompat.getDrawable(getResources(), android.R.drawable.btn_star, null);
    int strokeWidth = 5;
    int strokeColor = Color.parseColor("#ff0000ff");
    int fillColor = Color.parseColor("#ffff0000");
    GradientDrawable gD = new GradientDrawable();
    gD.setColor(fillColor);
    gD.setShape(GradientDrawable.OVAL);
    gD.setStroke(strokeWidth, strokeColor);
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{gD, innerDrawable});
    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(layerDrawable);

这是有效的,但由于某种原因,可拉伸的内部(星形)正在被拉伸:

enter image description here

解决方法:

您可以以编程方式执行此操作
在YourActivity.XMl中,像往常一样设置ImageView.

<ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/id1"
            android:src="@mipmap/ic_launcher_round"                                
            android:padding="15dp"/>

并在您的MainActivity.java中

    ImageView iV = (ImageView) findViewById(R.id.id1);
    int strokeWidth = 5;
    int strokeColor = Color.parseColor("#03dc13");
    int fillColor = Color.parseColor("#ff0000");
    GradientDrawable gD = new GradientDrawable();
    gD.setColor(fillColor);
    gD.setShape(GradientDrawable.OVAL);
    gD.setStroke(strokeWidth, strokeColor);
    iV.setBackground(gD);

setColor此处设置背景颜色,setStroke设置笔触宽度和笔触颜色.
我已经为颜色,宽度等创建了一些局部变量,使其更容易理解.
Resultenter image description here你增加填充量,圆圈的大小会增加.

标签:android-shapedrawable,android,android-drawable,layerdrawable
来源: https://codeday.me/bug/20190724/1522609.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有