在小部件中动态设置 ImageView 的圆角?

Rounded corners on dynamicly set ImageView in widget?(在小部件中动态设置 ImageView 的圆角?)

本文介绍了在小部件中动态设置 ImageView 的圆角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有配置活动的小部件,用户可以在其中从颜色选择器中选择小部件背景的颜色.我正在使用下面的方法,其中我有一个 ImageView 并创建了一个我在 ImageView 上动态设置的位图.

I have a widget with a config activity, where the user can select a color for the background of the widget from a color picker. I am using the method below where I have an ImageView and create a Bitmap which I dynamically set on the ImageView.

http://konsentia.com/2011/03/dynamically-changed-the-background-color-in-android-widgets/

public static Bitmap getBackground (int bgcolor)
{
try
    {
        Bitmap.Config config = Bitmap.Config.ARGB_8888; // Bitmap.Config.ARGB_8888 Bitmap.Config.ARGB_4444 to be used as these two config constant supports transparency
        Bitmap bitmap = Bitmap.createBitmap(2, 2, config); // Create a Bitmap

        Canvas canvas =  new Canvas(bitmap); // Load the Bitmap to the Canvas
        canvas.drawColor(bgcolor); //Set the color

        return bitmap;
    }
    catch (Exception e)
    {
        return null;
    }
}

然后

remoteViews.setImageViewBitmap(R.id.bgcolor, getBackground(bgcolor));

我想要做的是让用户也可以选择他们是否想要小部件上的圆角.是否可以动态更改颜色以及小部件是否具有圆角?从我看过的圆角示例中,您似乎需要知道视图的尺寸,以便在设置位图之前可以圆角边缘.我认为这在小部件中是不可能的……有什么想法吗?

What I want to do though is let the user also choose if they want rounded corners on the widget. Is it possible to dynamically change both the color and whether the widget has rounded corners? From the examples I have looked at for rounding corners it seems you need to know the dimensions of the View so that you can round the edges before setting the Bitmap. I don't think this is possible from within a widget though... any ideas?

推荐答案

有两种方法:

  1. 创建一个圆角/方角的位图(使用您现有的方法),该位图的大小与您想要的小部件的大小大致相同.如果用户调整小部件的大小或在具有您未考虑到的某些屏幕分辨率/DPI 的设备上使用它,则存在位图扭曲的风险
  2. 创建一些带圆角的白色9 补丁位图资源角和方角并使用 RemoteViews.setInt 更改小部件背景 ImageView 的颜色/透明度(需要 Froyo 或更高版本),例如

  1. Create a bitmap with rounded/square corners (using your existing method) that is roughly the size of the widget you want. This has risk of the bitmap distorting if the user resizes the widget or uses it on a device with some screen resolution/DPI you have not taken into consideration
  2. Create some white 9 patch bitmap resources with rounded corners and square corners and use RemoteViews.setInt to change the color/transparency of the widget background ImageView (requires Froyo or greater), e.g.

if(roundCorners)
    remoteViews.setImageViewResource(R.id.widget_background, R.drawable.round_corners);
else
    remoteViews.setImageViewResource(R.id.widget_background, R.drawable.square_corners);  

remoteViews.setInt(R.id.widget_background, "setColorFilter", someColor);
remoteViews.setInt(R.id.widget_background, "setAlpha", someAlphaLevel);

我已经使用了这两种方法并推荐 (2) 以获得最大的兼容性.

I've used both methods and recommend (2) for maximum compatibility.

这篇关于在小部件中动态设置 ImageView 的圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在小部件中动态设置 ImageView 的圆角?

基础教程推荐