Android 自定义图库视图,设置自己的边框

发布于 2024-11-03 11:41:50 字数 1571 浏览 6 评论 0原文

我已经实现了 GalleryView。我想在图库中选定的图像上显示边框图像。

Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));//, android.R.layout.simple_list_item_1, items));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int location,
                long arg3) {
            imageView.setImageResource(items.get(location));

            final ImageView iv = (ImageView) adapter.getSelectedView();
            iv.setBackgroundResource(R.drawable.large_button_sel_liner);
        }
    });

和我的 Adapter 类

class ImageAdapter1 extends ArrayAdapter<Integer> {

    private Context ctx;
    private List<Integer> items;

    public ImageAdapter1(Context context, int textViewResourceId,
            List<Integer> objects) {
        super(context, textViewResourceId, objects);
        items = objects;
        ctx = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView iv = new ImageView(ctx);
        iv.setImageResource(items.get(position));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));

        return iv;
    }
}!

完全混搭在一起。 在此处输入图像描述

I have implemented GalleryView. I want to display a border image on selected image from Gallery.

Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));//, android.R.layout.simple_list_item_1, items));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int location,
                long arg3) {
            imageView.setImageResource(items.get(location));

            final ImageView iv = (ImageView) adapter.getSelectedView();
            iv.setBackgroundResource(R.drawable.large_button_sel_liner);
        }
    });

And my Adapter class

class ImageAdapter1 extends ArrayAdapter<Integer> {

    private Context ctx;
    private List<Integer> items;

    public ImageAdapter1(Context context, int textViewResourceId,
            List<Integer> objects) {
        super(context, textViewResourceId, objects);
        items = objects;
        ctx = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView iv = new ImageView(ctx);
        iv.setImageResource(items.get(position));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));

        return iv;
    }
}!

It is totally mashed up.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-11-10 11:41:50

我已经使用选择器找到了解决方案。

我创建了 galleryselector.xml

<?xml version="1.0" encoding="utf-8"?>  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_selected="true" 
                    android:drawable="@drawable/large_button_sel_liner"/>
    <item android:drawable="@android:color/transparent" /></selector>

并将其设置在 getView() 的 Adapter 类中:

imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.galleryselector));

I have got the solution using Selector.

I have created galleryselector.xml

<?xml version="1.0" encoding="utf-8"?>  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_selected="true" 
                    android:drawable="@drawable/large_button_sel_liner"/>
    <item android:drawable="@android:color/transparent" /></selector>

and set it in Adapter class in getView():

imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.galleryselector));
歌枕肩 2024-11-10 11:41:50

受到 Nishant Shan 的回复的启发,我详细阐述了自己的回复解决方案:

首先,创建一个边框资源:common_galleryborder_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:radius="2dp"
    />
    <solid
        android:color="@android:color/transparent"
    />
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
   />
</shape>

然后创建一个使用此形状的选择器: common_gallerycurrentitem_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_selected="true"
        android:drawable="@drawable/common_galleryborder_shape"
    />
    <item
        android:drawable="@android:color/transparent"
    />
</selector>

最后,将此代码添加到您的适配器类中:

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

    Integer drawableId = items.get(position);
    Drawable drawable = ctx.getResources().getDrawable(drawableId);
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();

    imageView.setImageResource(drawableId);
    //sets image size to same size of true image
    imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    //add a padding for the border
    int padding = dipToPx(mContext, 2);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setBackgroundResource(R.drawable.common_gallerycurrentitem_selector);

    return imageView;
}

// ----------------------------------------- Private Methods

/**
 * Convert a dimension in dip to px
 * @param context
 * @param dip
 * @return px
 */
private int dipToPx(Context context, int dip) {
    return (int) (dip * context.getResources().getDisplayMetrics().density);
}    

此外,还可以设置 Gallery.setSpacing(int) 值以避免图库内的图像重叠。

Inspired by Nishant Shan's reply, I elaborate my own solution:

First of all, create a border resource: common_galleryborder_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:radius="2dp"
    />
    <solid
        android:color="@android:color/transparent"
    />
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
   />
</shape>

Then create a selector that use this shape: common_gallerycurrentitem_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_selected="true"
        android:drawable="@drawable/common_galleryborder_shape"
    />
    <item
        android:drawable="@android:color/transparent"
    />
</selector>

And, finally, add this code to your adapter class:

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

    Integer drawableId = items.get(position);
    Drawable drawable = ctx.getResources().getDrawable(drawableId);
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();

    imageView.setImageResource(drawableId);
    //sets image size to same size of true image
    imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    //add a padding for the border
    int padding = dipToPx(mContext, 2);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setBackgroundResource(R.drawable.common_gallerycurrentitem_selector);

    return imageView;
}

// ----------------------------------------- Private Methods

/**
 * Convert a dimension in dip to px
 * @param context
 * @param dip
 * @return px
 */
private int dipToPx(Context context, int dip) {
    return (int) (dip * context.getResources().getDisplayMetrics().density);
}    

In addition, is also possible set Gallery.setSpacing(int) value to avoid overlapping images inside the gallery.

无敌元气妹 2024-11-10 11:41:50

过去对我有用的是创建一个 Integer 来跟踪单击选择,然后如果位置与单击选择匹配,则在 getView() 方法中应用背景。所以(大致):

Integer selectedLocation = null;

@Override
        public void onItemClick(AdapterView<?> adapter, View view, int location,
                long arg3) {
            selectedLocation = location;
        }

然后在适配器视图中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ImageView iv = new ImageView(ctx);
    iv.setImageResource(items.get(position));
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(150,120));
    if(position == selectedLocation){
          iv.setBackgroundResource(R.drawable.large_button_sel_liner);
    }else{
          iv.setBackgroundResource(0);
    }

    return iv;
}

您可能需要做更多的工作来跟踪正确的选择(单击位置和位置可能不是要使用的正确变量),但否则类似的东西应该可以工作。

What has worked for me in the past is to create an Integer that tracks the click selection, and then apply the background in the getView() method if the position matches the click selection. So (roughly):

Integer selectedLocation = null;

@Override
        public void onItemClick(AdapterView<?> adapter, View view, int location,
                long arg3) {
            selectedLocation = location;
        }

and then in the Adapter View:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ImageView iv = new ImageView(ctx);
    iv.setImageResource(items.get(position));
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(150,120));
    if(position == selectedLocation){
          iv.setBackgroundResource(R.drawable.large_button_sel_liner);
    }else{
          iv.setBackgroundResource(0);
    }

    return iv;
}

You might have to do a bit more work to track the right selection (the click location and the position might not be the right variables to use), but otherwise something similar should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文