Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ImageSwitcher to realize a photo album function in Android

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use ImageSwitcher in Android to achieve a photo album function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use ImageSwitcher in Android to achieve a photo album function" bar!

The code is as follows:

There are two common snooping events about GridView:

GridView.setOnItemSelectedListener and gridView.setOnItemClickListener

The imageSwitcher.setFactory method is adopted for setting up ImageSwitcher for ImageSwitcher:

Public class MainActivity extends Activity {int [] imageId = new int [] {R.drawable.a0 imageId = new int [] {R.drawable.a1memo R.drawable.a2 > R.drawable.a4, R.drawable.a5 > R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a00R.drawable.a02,}; ImageSwitcher imageSwitcher; @ Override protected void onCreate (Bundle savedInstanceState) {drawable.a02) SetContentView (R.layout.activity_main); / / create a List object whose elements are Map List listitems = new ArrayList (); for (int I = 0; I < imageId.length; iTunes +) {Map listitem = new HashMap (); listitem.put ("image", imageId [I]); listitems.add (listitem);} / / get ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById (R.id.switcher) that displays the picture / / animate ImageSwitcher imageSwitcher.setFactory (new ViewSwitcher.ViewFactory () {@ Override public View makeView () {/ / create ImageView object ImageView imageView = new ImageView (MainActivity.this); imageView.setScaleType (ImageView.ScaleType.FIT_XY); imageView.setLayoutParams (new ImageSwitcher.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); / / return ImageView object return imageView }}); / / create a SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter (this,listitems,R.layout.cell,new String [] {"image"}, new int [] {R.id.image1}); GridView gridView = (GridView) findViewById (R.id.grid01); / / set adapter gridView.setAdapter (simpleAdapter) for gridView / / add list to select listening event gridView.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView parent, View view, int position, long id) {imageSwitcher.setVisibility (View.VISIBLE); imageSwitcher.setClickable (true); / / display the currently selected image imageSwitcher.setImageResource (imageID [position]) } @ Override public void onNothingSelected (AdapterView parent) {}}); / / add listeners gridView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {imageSwitcher.setVisibility (View.VISIBLE); imageSwitcher.setClickable (true)) whose list is clicked / / display the clicked image imageSwitcher.setImageResource (imageID [position]);}}); / / add a listening event imageSwitcher.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {imageSwitcher.setVisibility (View.GONE); imageSwitcher.setClickable (false);}}); imageSwitcher.setClickable (false);}} for imageSwitcher

There are a few points worth noting:

Because SimpleAdapter objects are based on Map, their Key must be the same, otherwise they cannot be generated.

About the flicker phenomenon: I just used the compressed picture, if it is loading the high-definition large image is likely to appear OOM phenomenon, this is that we need to compress the picture

Here, as usual, the cell file is just a simple ImageView. Be sure to set its id to be the same as where it is called in the activity, which is not described here.

Thank you for your reading, the above is the content of "how to use ImageSwitcher in Android to achieve a photo album function". After the study of this article, I believe you have a deeper understanding of how to use ImageSwitcher in Android to achieve a photo album function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report