Android: cartoon APP development notes: Scaling pictures in ListView by screen width

Source: Internet
Author: User
Tags transparent color

Android: cartoon APP development notes: Scaling pictures in ListView by screen width

1. listview

    
 

Where,
 android:divider="#00000000"
Set the interval color of the list items to transparent,
android:listSelector="#00000000"
Set no background color when you click an image (transparent color, no system background color)


Ii. piclist_item

         
  
 

Iii. PicListInfo

Package com. cartoon. adapters; import android. graphics. bitmap; public class PicListInfo {private Bitmap PicUrl; // image address // The get and set methods of the above attributes are as follows: public Bitmap getPicUrl () {return PicUrl ;} public void setPicUrl (Bitmap picUrl) {PicUrl = picUrl ;}}

Iv. PicListAdapter
Package com. cartoon. adapters; import java. util. arrayList; import com. cartoon. r; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. view. measureSpec; import android. view. viewGroup; import android. view. viewGroup. marginLayoutParams; import android. view. windowManager; import android. widget. arrayAdapter; import android. widget. imageView; import android. widget. listAdapter; import android. widget. listView; import android. widget. relativeLayout; import android. widget. textView; public class PicListAdapter extends ArrayAdapter
 
  
{Public PicListAdapter (Context context, int textViewResourceId, ArrayList
  
   
Objects) {super (context, textViewResourceId, objects) ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; View view View; windowManager wm = (WindowManager) getContext (). getSystemService (Context. WINDOW_SERVICE); int width = wm. getdefadisplay display (). getWidth (); if (convertView = null) {view = LayoutInflater. from (getContext ()). inflate (R. layout. piclist_item, null );
   
// Set the image size holder = new ViewHolder (); holder. piclist_item = (ImageView) view. findViewById (R. id. piclist_item); ViewGroup. layoutParams margin = new ViewGroup. layoutParams (holder. piclist_item.getLayoutParams (); RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (margin );
LayoutParams. height = (int) (width * 0.5677); // you can specify the Image height.
LayoutParams. width = width; // you can specify the image width.
LayoutParams. setMargins (15, 0, 15, 0 );
Holder. piclist_item.setLayoutParams (layoutParams );
Holder. piclist_item.setScaleType (ImageView. scaleType. FIT_START); view. setTag (holder);} else {view = convertView; holder = (ViewHolder) convertView. getTag ();} final PicListInfo singleoder = getItem (position); if (singleoder! = Null) {holder. piclist_item.setImageBitmap (singleoder. getPicUrl ();} return view;} public class ViewHolder {public ImageView piclist_item ;}}

Listview + adapter is used to solve the problem of table item height. The general method is to only display the image in the small area on the left of each table item. The project requires proportional scaling of the image to the screen width and height, so the screen width is obtained first.
   

WindowManager wm = (WindowManager) getContext()                .getSystemService(Context.WINDOW_SERVICE);                int width = wm.getDefaultDisplay().getWidth();                int height = wm.getDefaultDisplay().getHeight();
Then adjust the image size, and the comments are clearly written:

                holder.piclist_item = (ImageView)view.findViewById(R.id.piclist_item);             ViewGroup.LayoutParams margin9 = new ViewGroup.LayoutParams(holder.piclist_item.getLayoutParams());          RelativeLayout.LayoutParams layoutParams  = new RelativeLayout.LayoutParams(margin);
LayoutParams. height = (int) (width * 0.5677); // you can specify the Image height.
LayoutParams. width = width; // you can specify the image width.
LayoutParams. setMargins (15, 0, 15, 0); // The left and right margins of 15.0px, consistent with the main frame
Holder. piclist_item.setLayoutParams (layoutParams );
Holder. piclist_item.setScaleType (ImageView. ScaleType. FIT_START); // scale up or down to the width of the View, and then display it on top.
View. setTag (holder );

Given that the width and height ratio of an image are known, you only need to multiply the height by the corresponding proportion when zooming the width of the screen. Or scale by ScaleType.

Appendix: ScaleType attributes

1) The CENTER is centered based on the original size of the image. When the image length and width exceed the upper width of the View, the CENTER part of the screenshot is displayed. 2) CENTER_CROP is scaled up to CENTER the image size, so that the length and width of the image are equal to or greater than the length and width of the View. 3) CENTER_INSIDE displays the entire content of the image in the center, scale down or the original size to make the image length and width equal to or less than the length and width of the view 4) FIT_CENTER scales up or down the image to the width of the View, and then center the image to 5) FIT_STAR scales up or down the image to the width of the View, and then sets the top to show 6) FIT_END scales up or down the image to the width of the View, and then places it in the bottom to show 7) FIT_XY scales the image proportionally to fill the entire View

If



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.