在小部件动态地设置ImageView的圆角? [英] Rounded corners on dynamicly set ImageView in widget?

查看:894
本文介绍了在小部件动态地设置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-changing-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?

推荐答案

有这样做的2种方法:


  1. 创建带有圆角/方角(使用现有的方法)的位图,它大致是你想要的小部件的大小。这有位图扭曲的风险,如果用户调整Widget或使用它的一些屏幕分辨率/ DPI的设备上,你有没有考虑到

  2. 创建一些白色 9补丁位圆润的资源角落和广场的角落,并使用<一个href=\"http://developer.android.com/reference/android/widget/RemoteViews.html#setInt%28int,%20java.lang.String,%20int%29\"相对=nofollow> 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的圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆