gpt4 book ai didi

Android:ImageView 失去透明度

转载 作者:行者123 更新时间:2023-11-29 02:09:06 26 4
gpt4 key购买 nike

我有一个来自具有头像的用户的博客文章列表。由于某些设计原因,我需要将方形头像图像裁剪成圆形图像。

这是我的 ListView xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listViewBlogs"
style="@style/CustomListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:divider="@drawable/divider" >
</ListView>
</LinearLayout>

<style name="CustomListView">
<item name="android:fadingEdge">none</item>
<item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:divider">@null</item>
<item name="android:listSelector">@android:color/transparent</item>
</style>

这是我的ListViewItem xml(博文内容从示例中删除,只保留作者姓名和头像)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip">

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageAvatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/avatar_default2"
android:focusable="false"
android:background="@color/transparent"
android:layerType="hardware"
/>

<TextView
android:id="@+id/textAuthor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="TextView"
android:textColor="@color/text_dark_grey"
android:textSize="20dip"
android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

这是我的适配器的 fragment 。图像通过互联网下载并缓存到本地存储中。然后裁剪出的像素填充透明

class BlogsAdapter extends ArrayAdapter<BlogItem>{    
//...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

if(v == null){
LayoutInflater inf = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.blogs_list_item_1, null);
}


BlogItem item = getItem(position);

if(item != null){

TextView textAuthor = (TextView)v.findViewById(R.id.textAuthor);
if(textAuthor != null){
textAuthor.setText(item.author_name);
}

ImageView imageAvatar = (ImageView)v.findViewById(R.id.imageAvatar);
if(imageAvatar != null){

//Load image from cache
BitmapFactory.Options op = new Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap imageFromCache = BitmapFactory.decodeFile(item.imagepath, op);

//Crop round. This is not quickest one solution to do it here, but it is here to make test case clear
Bitmap bmp2 = imageFromCache.copy(Bitmap.Config.ARGB_8888, true);
int mw = bmp2.getWidth();
int mh = bmp2.getHeight();
int wc = mw/2;
int hc = mh/2;

for(int i=0; i<mw;i++){
for(int j=0; j<mh;j++){
if( Math.sqrt( (i-wc)*(i-wc) + (j-hc)*(j-hc) ) >=(wc-2) ){
bmp2.setPixel(i, j, Color.TRANSPARENT);
}
}
}

//Set cropped image into view
imageView.setBackgroundColor(0);
imageView.setImageBitmap(bmp2);

}
return v;
}
}

效果不错

Good screenshot

但是当我滚动 ListView 时,透明度会降低

Not so good one

我试过:

  • android:cacheColorHint=@android:color/transparent 设置为 ListViewStyle
  • imageView.setBackgroundColor(0);

有什么想法吗?

最佳答案

您是否尝试过在您的 ListView 标签中执行以下操作,

android:cacheColorHint="#00000000"

关于Android:ImageView 失去透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8486031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com