Android Gallery与ImageSwitcher使用

例题2-14

图片自己存放在工程中

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2     <!--suppress ALL -->
 3 <TableLayout android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:layout_gravity="center"
 6     xmlns:android="http://schemas.android.com/apk/res/android">
 7     <TableRow>
 8         <Gallery android:layout_height="wrap_content"
 9             android:layout_width="wrap_content"
10             android:id="@+id/gallery1"
11             android:spacing="10sp"
12             android:unselectedAlpha="0.5" />
13     </TableRow>
14     <TableRow>
15         <TextView android:layout_height="150dip"
16             android:layout_width="150dip"/>
17     </TableRow>
18     <TableRow>
19         <ImageSwitcher android:id="@+id/imageswitcher1"
20             android:layout_width="350dip"
21             android:layout_height="450dip">
22         </ImageSwitcher>
23     </TableRow>
24 </TableLayout>

Mainactivity.java

 1 package com.example.hello;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.view.animation.AnimationUtils;
 9 import android.widget.AdapterView;
10 import android.widget.BaseAdapter;
11 import android.widget.Gallery;
12 import android.widget.ImageSwitcher;
13 import android.widget.ImageView;
14 import android.widget.ViewSwitcher;
15 
16 
17 public class MainActivity extends AppCompatActivity {
18     Gallery gallery;
19     ImageSwitcher imageSwitcher;
20     private int[] images={
21             R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5,
22             R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9,
23             R.drawable.p10, R.drawable.p11, R.drawable.p12, R.drawable.p1
24     };
25 
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_main);
30         imageSwitcher=(ImageSwitcher) findViewById(R.id.imageswitcher1);
31         imageSwitcher.setFactory(new viewFactory());
32         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
33         imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
34         imageSwitcher.setImageResource(R.drawable.p1);
35         gallery=(Gallery) findViewById(R.id.gallery1);
36         gallery = (Gallery)findViewById(R.id.gallery1);
37         gallery.setOnItemClickListener(new onItemSelectedListener());
38         gallery.setSpacing(10);
39         gallery.setAdapter(new baseAdapter());
40     }
41 
42     class viewFactory implements ViewSwitcher.ViewFactory{
43         @Override
44         public View makeView() {
45             ImageView imageView=new ImageView(MainActivity.this);
46             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
47             return imageView;
48         }
49     }
50 
51     class onItemSelectedListener implements AdapterView.OnItemClickListener {
52         @Override
53         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
54             imageSwitcher.setImageResource((int)gallery.getItemIdAtPosition(position));
55         }
56 
57 
58         public void onNothingSelected(AdapterView<?> arg0){ }
59     }
60 
61     class baseAdapter extends BaseAdapter
62     {
63         public int getCount(){
64             return images.length;
65         }
66         public Object getItem(int position){
67             return null;
68         }
69 
70         @Override
71         public long getItemId(int position) {
72             return images[position];
73         }
74 
75         public View getView(int position, View convertVierw, ViewGroup parent)
76         {
77             ImageView imageView=new ImageView(MainActivity.this);
78             imageView.setImageResource(images[position]);
79             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
80             imageView.setLayoutParams(new Gallery.LayoutParams(150,150));
81             return imageView;
82         }
83     }
84 }

 

posted @ 2020-09-25 10:48  冯寳寳  阅读(166)  评论(0编辑  收藏  举报