别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述Android基础TOP6_2:Gallery +Image完成画廊相关的知识,希望能为你提供帮助。
Activity:
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" 6android:paddingBottom="@dimen/activity_vertical_margin" 7android:paddingLeft="@dimen/activity_horizontal_margin" 8android:paddingRight="@dimen/activity_horizontal_margin" 9android:paddingTop="@dimen/activity_vertical_margin" 10tools:context="com.example.top6_2.MainActivity" > 11 < Gallery 12android:id="@+id/gly" 13android:layout_height="wrap_content" 14android:layout_width="fill_parent" 15android:animationDuration="1000" 16android:spacing="20dp" 17android:unselectedAlpha="0.5" 18/> 19< ImageView 20android:id="@+id/img" 21android:layout_height="wrap_content" 22android:layout_width="wrap_content" 23/> 24 25 < /LinearLayout>
java:
文章图片
MinActivity:
1 package com.example.top6_2; 2 3 import android.R.integer; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemClickListener; 11 import android.widget.AdapterView.OnItemSelectedListener; 12 import android.widget.Gallery; 13 import android.widget.ImageView; 14 15 16 public class MainActivity extends Activity { 17private Gallery gal; 18private ImageView iv; 19private ImageAdapter adapter; 20private static final int[]imgslds={ 21R.drawable.bjl, 22R.drawable.dfhgc, 23R.drawable.fg, 24R.drawable.gj 25}; 26@Override 27protected void onCreate(Bundle savedInstanceState) { 28super.onCreate(savedInstanceState); 29setContentView(R.layout.activity_main); 30iv=(ImageView) findViewById(R.id.img); 31gal=(Gallery) findViewById(R.id.gly); 32adapter=new ImageAdapter(this); 33gal.setAdapter(adapter); 34gal.setOnItemSelectedListener(gaILidstener); 35} 36private Gallery.OnItemSelectedListener gaILidstener=new Gallery.OnItemSelectedListener() { 37 38@Override 39public void onItemSelected(AdapterView< ?> parent, View view, 40int position, long id) { 41// TODO Auto-generated method stub 42iv.setImageResource(imgslds[position]); 43} 44 45@Override 46public void onNothingSelected(AdapterView< ?> parent) { 47// TODO Auto-generated method stub 48 49} 50}; 51}
ImagrAdapter:
1 package com.example.top6_2; 2 3 import android.R.integer; 4 import android.content.Context; 5 import android.view.Gravity; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.Gallery; 10 import android.widget.ImageView; 11 12 public class ImageAdapter extends BaseAdapter { 13private Context context; 14 15private static final int[]imgslds={ 16R.drawable.bjl, 17R.drawable.dfhgc, 18R.drawable.fg, 19R.drawable.gj 20 }; 21public ImageAdapter(Context c){ 22context=c; 23 24} 25public int getCount() { 26// TODO Auto-generated method stub 27// 28return imgslds.length; 29 30} 31 32@Override 33public Object getItem(int position) { 34// TODO Auto-generated method stub 35return position; 36} 37 38@Override 39public long getItemId(int position) { 40// TODO Auto-generated method stub 41return position; 42} 43 44@Override 45public View getView(int position, View convertView, ViewGroup parent) { 46// TODO Auto-generated method stub 47ImageView iv=new ImageView(context); 48iv.setImageResource(imgslds[position]); 49iv.setScaleType(ImageView.ScaleType.FIT_CENTER); 50iv.setLayoutParams(new Gallery.LayoutParams(120,80)); 51return iv; 52} 53 54 }
效果图:
【Android基础TOP6_2:Gallery +Image完成画廊】
文章图片
推荐阅读
- Android Studio创建AIDL文件并实现进程间通讯
- Android中 string.xml资源 如何添加参数()
- 安卓的异步下载(Asynchttpclient以及Volley)
- SDK manager.exe 运行时报错(系统找不到指定的文件 android.bat)
- Android基础TOP6_1:FrameLyayout和ImageView制作层叠图片
- applicationContext.xml
- 为android编译libsocket的脚本
- Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins问题的解决
- PCIE_DMA实例二(xapp1052的EDK仿真)