Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: ImageView loses transparency
    primarykey
    data
    text
    <p>I have a list of blog posts from users with avatars. Cause of some design reasons I need to crop square avatar images into circle images.</p> <p>This is my ListView xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; &lt;style name="CustomListView"&gt; &lt;item name="android:fadingEdge"&gt;none&lt;/item&gt; &lt;item name="android:cacheColorHint"&gt;@android:color/transparent&lt;/item&gt; &lt;item name="android:divider"&gt;@null&lt;/item&gt; &lt;item name="android:listSelector"&gt;@android:color/transparent&lt;/item&gt; &lt;/style&gt; </code></pre> <p>This is my ListViewItem xml (blog post content is removed from example, only author name and avatar remains)</p> <pre><code>&lt;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"&gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is fragment from my adapter. Images are downloaded via Internet and cached into local storage. Then cropped pixels are filled with transparent</p> <pre><code>class BlogsAdapter extends ArrayAdapter&lt;BlogItem&gt;{ //... @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&lt;mw;i++){ for(int j=0; j&lt;mh;j++){ if( Math.sqrt( (i-wc)*(i-wc) + (j-hc)*(j-hc) ) &gt;=(wc-2) ){ bmp2.setPixel(i, j, Color.TRANSPARENT); } } } //Set cropped image into view imageView.setBackgroundColor(0); imageView.setImageBitmap(bmp2); } return v; } } </code></pre> <p>It works good enougth</p> <p><img src="https://i.stack.imgur.com/m6Ijs.png" alt="Good screenshot"></p> <p>But when I scroll listview, transparency looses</p> <p><img src="https://i.stack.imgur.com/UQQSF.png" alt="Not so good one"></p> <p>I tried:</p> <ul> <li>android:cacheColorHint=@android:color/transparent is setted into ListViewStyle</li> <li>imageView.setBackgroundColor(0);</li> </ul> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload