2

我创建了简单的 gridview 应用程序。现在我想为我的图像创建发光效果,请帮助我如何为我的网格视图图像创建发光效果?如果有人知道,请给我一些想法和示例代码....

这是我现在的截图:

在此处输入图像描述

这是我预期的发光效果截图: 在此处输入图像描述

源代码:

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
<GridView 
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />
</LinearLayout>

GridviewExampleActivity.java

public class GridviewExampleActivity extends Activity
{

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
}

public class ImageAdapter extends BaseAdapter{
private Context mContext;

public ImageAdapter(Context c){
mContext = c;
}

public int getCount()
{

    return mThumbIds.length;
 }

public Object getItem(int position)
{ 
    return null;

}

public long getItemId(int position)
{
return 0;
                                }
// create a new ImageView for each item referenced by the Adapter

public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;

if (convertView == null)
{
 imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
                                                }
imageView.setImageResource(R.drawable.icon);

return imageView;
                                }
private Integer[] mThumbIds = {
        R.drawable.icon, R.drawable.icon,
        R.drawable.icon, R.drawable.icon,
        R.drawable.icon, R.drawable.icon,
        R.drawable.icon, R.drawable.icon,
        R.drawable.icon, R.drawable.icon};
}   

}

发光效果屏幕截图:![在此处输入图像描述][3]

4

2 回答 2

7

您可以使用以下代码使自定义视图中的每个图像发光

图像适配器的 getView() 函数应该是这样的:

public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;

if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8); 
}
else
{
   imageView = (ImageView) convertView;
   Bitmap Image=BitmapFactory.decodeResource(mContext.getResources(),mThumbIds[position]);
        Image=Image.copy(Bitmap.Config.ARGB_8888,true);
        Paint paint=new Paint();
        paint.setDither(true);
        paint.setFilterBitmap(true);
        Bitmap glow=BitmapFactory.decodeResource(mContext.getResources(), R.drawable.glow_image);
        Bitmap bitmap=Bitmap.createBitmap(Image.getWidth(),Image.getHeight(), Config.ARGB_8888);
        Canvas canvas=new Canvas(bitmap);

        canvas.drawBitmap(glow, new Rect(0,0,glow.getWidth(),glow.getHeight()), new Rect(0,0,Image.getWidth(),Image.getHeight()),paint);
        canvas.drawBitmap(Image, new Rect(0,0,Image.getWidth(),Image.getHeight()), new Rect(0+5,0+5,Image.getWidth()-5,Image.getHeight()-5),paint);



        imageView.setImageBitmap(bitmap);


   return imageView;
   }

R.drawable.glow_image 是可以用作生长效果图像的 png 图像

发光.png

于 2012-05-14T10:28:10.453 回答
3

检查此链接...它是一个自定义选择器

或者只是你可以使用这个

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true" android:drawable="@drawable/pressedback" />
   <item android:state_focused="true" android:drawable="@drawable/focusedback" />
   <item android:state_selected="true" android:drawable="@drawable/focusedback" />
</selector>
于 2012-05-14T09:18:43.710 回答