如何在android中单击imageview时突出显示图像视图的边框

Niv*_*tha 2 android android-layout android-imageview android-fragments android-studio

我在android中的水平视图中有图像列表.如果我单击特定图像的图像视图必须以编程方式突出显示边框颜色.如何为图像视图的突出显示边框执行此操作.提前致谢.

想要显示如下所述的边框

当用户单击ImageView的单个图像时,我想显示像ImageView的这个边框.

horizontalimage=(LinearLayout)findViewById(R.id.linearimage);
                                               // final RelativeLayout r1=(RelativeLayout)findViewById(R.id.relative_border);
                                             //   frame=(FrameLayout)findViewById(R.id.framelayout
  if(multipleimage.length()>0) {
  for (int j = 0;j<multipleimage.length();j++)
    {
    pimages=multipleimage.getJSONObject(j);
    JSONObject oneimage=multipleimage.getJSONObject(0);
    ii= new ImageView(singleshooppingcart.this);
    multipleimages=(ImageView)findViewById(R.id.singleimage);

   ii.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
   LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
   image.width=100;
   image.height=1
   image.setMargins(5,0,0,0);


   final String multimgs=pimages.getString("original_res");
   String oneimg=oneimage.getString("original_res");


   String[] img2 = multimgs.split("\\.");
   String imagone=productpath + alertObj.getString("seller_id")+  '/' + img2[0] + '(' + '1' + '0' + '0' + ')' + '.' + img2[1];
   String singleiamges=productpath + alertObj.getString("seller_id")+  '/' + oneimg;
// displayimages=productpath + alertObj.getString("seller_id")+  '/' + multimgs[];
   YelloPage.imageLoader.displayImage(imagone, ii, options);
   YelloPage.imageLoader.displayImage(singleiamges, multipleimages, options);

   ii.setLayoutParams(image);
 // ii.setBackgroundResource(R.drawable.imgviewpress);
 //  ii.setBackground(R.drawable.imgviewpress);
                                                       /* GradientDrawable gd = new GradientDrawable();
     gd.setColor(R.drawable.imageviewhighlight); // Changes this drawbale to use a single color instead of a gradient
     gd.setCornerRadius(5);
                  // gd.setStroke(1, 0xFF000000);
     ii.setBackgroundDrawable(gd);*/

     horizontalimage.addView(ii);
     ii.setOnClickListener(new View.OnClickListener() {
       @Override
   public void onClick(View view) {

   Drawable highlight = getResources().getDrawable( R.drawable.imgviewpress);
   ii.setBackground(highlight);
   int indexOfImage = horizontalimage.indexOfChild(view);
   String img1=String.valueOf(indexOfImage);



// Toast.makeText(getApplicationContext(),img1,Toast.LENGTH_LONG).show();
    try {
         images = multipleimage.getJSONObject(Integer.parseInt(img1)).getString(String.valueOf("original_res"));
        } catch (JSONException e) {
         e.printStackTrace();
        }
       // Toast.makeText(getApplicationContext(),images,Toast.LENGTH_LONG).show();
      // multipleimages.setImageResource(indexOfImage);
         try 
         YelloPage.imageLoader.displayImage(productpath + alertObj.getString("seller_id")+"/"+images, multipleimages, options);
         } catch (JSONException e) {
         e.printStackTrace();
         }

        // String img1=String.valueOf(indexOfImage);
     // YelloPage.imageLoader.displayImage(displayimages[indexOfImage], multipleimages, options);
 }
             });
Run Code Online (Sandbox Code Playgroud)

Akh*_*man 12

  1. 在drawable文件夹中创建一个可绘制文件.

highlight.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <corners android:radius="0dp"/>
    <solid android:color="@android:color/transparent"/>
    <stroke android:color="#FFC830"
        android:width="3dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
  1. 您的活动中的ImageView

    <ImageView
    android:id="@+id/iv_test"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:padding="3dp"
    android:src="@drawable/yourImageName"
    android:tint="@color/colorAccent"/>
    
    Run Code Online (Sandbox Code Playgroud)

这里填充很重要.

  1. 内部活动代码

    imv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable highlight = getResources().getDrawable( R.drawable.highlight);
            imv.setBackground(highlight);
            //
            //Do your other stuff here.
            //
        }
    });
    
    Run Code Online (Sandbox Code Playgroud)

如果要删除背景,请使用以下代码:

imv.setBackground(null);
Run Code Online (Sandbox Code Playgroud)

在活动xml中,填充属性很重要,因为高亮背景将采用与imageview相同的大小.因此,如果imageview中有任何图像,我们将无法看到背景/突出显示.因此,padding属性将图像向内推一点,让高亮显示.

产量

第一 单击时

UPDATE

在你的代码中实现View.OnClickListener.

并更改您的onClick()方法

@Override
public void onClick(View v) {
    Drawable highlight = getResources().getDrawable( R.drawable.highlight );
    for (int j=0;j<ii.length;j++)
    {
        if (ii[j].getBackground()!=null) {
            ii[j].setBackground(null);
        }
    }
    v.setBackground(highlight);

    //
    //Do what you want to do when clicking on the imageview
    //
}
Run Code Online (Sandbox Code Playgroud)

现在你正在使用

ii=new ImageView(singleshooppingcart.this);
Run Code Online (Sandbox Code Playgroud)

使它成为一个数组并像这样使用它

ii[i]=new ImageView(singleshooppingcart.this);
Run Code Online (Sandbox Code Playgroud)

改变这个

ii.setOnClickListener(new View.OnClickListener() {
   @Override
 public void onClick(View view) {

 Drawable highlight = getResources().getDrawable(R.drawable.imgviewpress);
ii.setBackground(highlight);
Run Code Online (Sandbox Code Playgroud)

    ii[i].setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)

这里我是循环变量.

这样,您将拥有所有imageView的对象.所有这些图像视图都将包含我们设置的ClickEvent.