在 android 中单击图像视图时如何突出显示图像视图的边框
How to highlight a border of an Image View when I clicked imageview in android
我在 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);
}
});
将图像放在 RelativeLayout
中并添加 1dp 的边距并在开始时使其不可见。
那么
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relativeLayout.SetVisibility(View.VISIBLE)
}
});
首先,您的 imageView 保持相对布局。例如
<RelativeLayout
android:id="@+id/relative_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="38dp"
android:background="#FFFFFF"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
然后添加以下 java 代码。像这样
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_border);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.setBackgroundColor(0xFF00FF00);
}
});
创建一个带有线性布局的自定义图像视图和带有一些填充的图像
基本上改变布局的颜色,这将为图像视图提供边框
点击使用这个,
customImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//use either v.getchild(position) or v.findViewById(R.id.borderlayout)
LinearLayout borderlayout = v.getChild(0);
// change the border color or visibility you like to do
}
});
尝试使用GradientDrawable
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
Imageview imgs= (Imageview )findViewById(R.id.imgviw);
imgs.setBackgroundDrawable(gd);
- 在 drawable 文件夹中创建一个 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>
ImageView 在你的 activity
<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"/>
这里填充很重要。
里面activity代码
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.
//
}
});
如果要删除背景,请使用此代码:
imv.setBackground(null);
在activity xml中,padding 属性很重要,因为高亮背景将采用与图像视图相同的大小。因此,如果图像视图中有任何图像,我们将无法看到 background/highlight。所以 padding 属性将图像向内推了一点,让高光可见。
输出
更新
在你的代码中实现 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
//
}
现在您正在使用
ii=new ImageView(singleshooppingcart.this);
把它做成一个数组然后像这样使用它
ii[i]=new ImageView(singleshooppingcart.this);
改变这个
ii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable highlight = getResources().getDrawable(R.drawable.imgviewpress);
ii.setBackground(highlight);
到
ii[i].setOnClickListener(this);
这里的i是循环变量
这样您将拥有所有 imageView 的对象。所有这些图像视图都将有一个我们设置的 ClickEvent。
我在 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);
}
});
将图像放在 RelativeLayout
中并添加 1dp 的边距并在开始时使其不可见。
那么
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relativeLayout.SetVisibility(View.VISIBLE)
}
});
首先,您的 imageView 保持相对布局。例如
<RelativeLayout
android:id="@+id/relative_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="38dp"
android:background="#FFFFFF"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
然后添加以下 java 代码。像这样
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_border);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.setBackgroundColor(0xFF00FF00);
}
});
创建一个带有线性布局的自定义图像视图和带有一些填充的图像 基本上改变布局的颜色,这将为图像视图提供边框
点击使用这个,
customImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//use either v.getchild(position) or v.findViewById(R.id.borderlayout)
LinearLayout borderlayout = v.getChild(0);
// change the border color or visibility you like to do
}
});
尝试使用GradientDrawable
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
Imageview imgs= (Imageview )findViewById(R.id.imgviw);
imgs.setBackgroundDrawable(gd);
- 在 drawable 文件夹中创建一个 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>
ImageView 在你的 activity
<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"/>
这里填充很重要。
里面activity代码
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. // } });
如果要删除背景,请使用此代码:
imv.setBackground(null);
在activity xml中,padding 属性很重要,因为高亮背景将采用与图像视图相同的大小。因此,如果图像视图中有任何图像,我们将无法看到 background/highlight。所以 padding 属性将图像向内推了一点,让高光可见。
输出
更新
在你的代码中实现 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
//
}
现在您正在使用
ii=new ImageView(singleshooppingcart.this);
把它做成一个数组然后像这样使用它
ii[i]=new ImageView(singleshooppingcart.this);
改变这个
ii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable highlight = getResources().getDrawable(R.drawable.imgviewpress);
ii.setBackground(highlight);
到
ii[i].setOnClickListener(this);
这里的i是循环变量
这样您将拥有所有 imageView 的对象。所有这些图像视图都将有一个我们设置的 ClickEvent。