我正在尝试在horizontalscrollview内的图像视图链周围添加分隔符/边框。
我的应用程序将显示特定文件夹中的所有图像,并将它们分别加载到图像视图中,该图像视图放置在水平横向视图中。
但是,大多数图像具有相同的背景颜色,因此很难看到它们彼此分开。我想在imageview上添加某种边框,或者在它们之间添加一个简单的分隔符。
我的横向布局代码:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MyHorizontalLayout extends LinearLayout {
Context myContext;
ArrayList<String> itemList = new ArrayList<String>();
public MyHorizontalLayout(Context context) {
super(context);
myContext = context;
}
public MyHorizontalLayout(Context context, AttributeSet attrs) {
super(context, attrs);
myContext = context;
}
public MyHorizontalLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
myContext = context;
}
void add(String path){
int newIdx = itemList.size();
itemList.add(path);
addView(getImageView(newIdx));
}
ImageView getImageView(final int i){
Bitmap bm = null;
if (i < itemList.size()){
bm = decodeSampledBitmapFromUri(itemList.get(i), 220, 220);
}
ImageView imageView = new ImageView(myContext);
imageView.setLayoutParams(new LayoutParams(500, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm);
imageView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String path = itemList.get(i);
DeviceControlActivity.m_Text = path.substring(path.lastIndexOf("/")+1);
Toast.makeText(myContext,
"Selected - " + DeviceControlActivity.m_Text,
Toast.LENGTH_SHORT).show();
}});
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
答案 0 :(得分:1)
尝试在imageView的左侧留下1dp的填充。如果您希望边距有一些颜色设置为该imageView的背景颜色。
答案 1 :(得分:0)
感谢Velmurugan V,
将<table>
<tr>
<td style="line-height:30px;border-collapse: collapse; padding:0;background-color:red; font-family: calibri light; font-size:70px; vertical-align: top; padding:0px !important;">
text
</td>
</tr>
</table>
添加到
imageView.setPadding(5,0,5,0);
这会在创建图像视图时设置每个imageView的填充。
答案 2 :(得分:0)
由于您正在扩展LinearLayout
,最简单的事情就是致电setShowDividers。