2013年8月11日星期日

Android

 

Introduction

 

this blog explain Android development, Gallery controls, which is a gallery view for showing a group of pictures, with a fixed position in the middle of the horizontal scrolling list shows the list item view. Android's latest API documentation to understand that, in Android4.1 (Android API level16), no longer in favor of using this control, but also to exert support, so here briefly explain how to use. In this blog, you will learn Gallery of some commonly used properties, methods, and style settings, the final will be a small example to show how to use the Gallery showcase a gallery effect.

 

 

Gallery

 

Gallery with Spinner two controls have a common parent class: AbsSpinner, means Gallery and Spinner is a list box. The difference between them is the Spinner shows a vertical list selection box, and Gallery shows a horizontal list selection box, and support horizontal sliding effect. Gallery there is a difference with Spinner: Spinner's role is for users to choose, and Gallery will allow users to view by dragging on one, the next list item. For Spinner, another one on my blog to explain in detail, interested friends can see: Android - UI's Spinner .

 

Here are some common XML attributes Gallery, Android also provides for the corresponding attribute getter, setter methods:

 
      
  • android: animationDuration: Set list items when switching duration of the animation, the use of milliseconds.
  •   
  • android: gravity: Set list items on its way.
  •   
  • android: spacing: Set Gallery inner spacing between list items.
  •   
  • android: unselectedAlpha: settings have not been selected list item transparency, the range is from 0 to 1, a float number, the closer to 0 the more transparent.
  •  
 

as a list box, which, Spinner, declaring an event is defined in the AdapterView class, there are several common event:

 
      
  • AdapterView.OnItemCLickListener: triggered when a list item is clicked.
  •   
  • AdapterView.OnItemLongClickListener: list items triggered by a long time.
  •   
  • AdapterView.OnItemSelectedListener: triggered when a list item is selected.
  •  
 

Gallery's data binding

 

Gallery's own usage is very simple, basically similar with Spinner usage, as long as it provides an Adapter adapter can be. If you need to respond to user actions on the Gallery, you can monitor the number of events described above.

 

Adapter adapter concept, I have to explain another one blog, interested friends can see, Android - UI's AutoCompleteTextView . And for the Gallery Adapter adapter, although you can directly implement Adapter interface, but generally recommend an abstract class inherits another: BaseAdapter , it has achieved a number of commonly used methods. For BaseAdapter concerned, it must implement several inherited method, the following first look at these methods:

 
      
  • int getCount (): current adapter exists in how many data items.
  •   
  • Object getItem (int position): returns the location of the data set specified data item.
  •   
  • long getItemId (int position): returns the data set specified location data row Id.
  •   
  • View getView (int position, View convertView, ViewGroup parent): Returns a view of the data set used to display the data items specified location.
  •  
 

For BaseAdapter the getView () method, where the emphasis explain. After the binding Adapter adapter, the system will getCount () method returns the data items, the cycle call getView () method that returns the location of each position display view, so for an Adapter adapter, the main amount of code in the getView () method. Because BaseAdapter the getView () method returns a View data, showing the effect of the general custom BaseAdapter will be used to do the parent class, which inherits implement methods to achieve custom View to show in the UI interface,

 

 

sample program

 

following examples illustrate through a knowledge points mentioned above, in this sample program, using a Gallery to display a group of pictures gallery effect, most of the projects are also used in this effect, but it shows that a View, so not only can be used to display pictures, here is a picture presentation. In the sample program also declares a ImageView, used to display the selected image from the Gallery, for ImageView usage, see another one blog: ImageView use , not here in tired out.

 

because it is a picture gallery display effect, so it is necessary to prepare a set of images that put res / drawable-hdpi directory, these pictures will be packaged together with the project source code in.

 

 

layout code:

 
  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools
="http://schemas.android.com/tools"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="vertical"
android:paddingBottom
="@dimen/activity_vertical_margin"
android:paddingLeft
="@dimen/activity_horizontal_margin"
android:paddingRight
="@dimen/activity_horizontal_margin"
android:paddingTop
="@dimen/activity_vertical_margin"
tools:context
=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="Gallery Demo" />
<!-- 定义一个Gallery,其中动画换进3秒,数据项间隔3dp,透明度为50% -->
<Gallery
android:id="@+id/gallery1"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:animationDuration
="3000"
android:spacing
="3dp"
android:unselectedAlpha
="0.5" />

<ImageView
android:id="@+id/image1"
android:layout_width
="320dp"
android:layout_height
="250dp"
android:scaleType
="fitXY" />

</LinearLayout>
 
 

implementation code:

 
  
  1 package com.bgxt.gallerydemo; 
2
3 import android.os.Bundle;
4 import android.app.Activity;
5 import android.content.Context;
6 import android.content.res.TypedArray;
7 import android.view.Menu;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.AdapterView;
11 import android.widget.AdapterView.OnItemClickListener;
12 import android.widget.BaseAdapter;
13 import android.widget.Gallery;
14 import android.widget.ImageView;
15
16 public class MainActivity extends Activity {
17 private int[] imagesIDs = new int[] { R.drawable.bmp1, R.drawable.bmp2,
18 R.drawable.bmp3, R.drawable.bmp4, R.drawable.bmp5, R.drawable.bmp6,
19 R.drawable.bmp7, R.drawable.bmp8, R.drawable.bmp9, R.drawable.bmp10 };
20 private Gallery gallery;
21 private ImageView image;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 gallery = (Gallery) findViewById(R.id.gallery1);
28 image = (ImageView) findViewById(R.id.image1);
29 gallery.setAdapter(new ImageAdapter(MainActivity.this));
30 gallery.setOnItemClickListener(new OnItemClickListener() {
31
32 @Override
33 public void onItemClick(AdapterView<?> parent, View view,
34 int position, long id) {
35 image.setImageResource(imagesIDs[position]);
36 }
37 });
38 }
39
40 // 声明一个BaseAdapter
41 public class ImageAdapter extends BaseAdapter {
42 // 使用Adapter的上下文变量
43 Context context;
44 // 背景样式的Id
45 int itemBackground;
46
47 public ImageAdapter(Context c) {
48 // 在构造函数中传递需要使用这个Adapter的上下文变量
49 context = c;
50 // 通过XML资源中定义的样式,设定背景
51 TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
52 itemBackground = a.getResourceId(
53 R.styleable.Gallery1_android_galleryItemBackground, 0);
54 a.recycle();
55 }
56
57 @Override
58 public int getCount() {
59 // 返回当前数据集中数据的个数
60 return imagesIDs.length;
61 }
62
63 @Override
64 public Object getItem(int position) {
65 // 返回数据集中,当前position位置的数据
66 return imagesIDs[position];
67 }
68
69 @Override
70 public long getItemId(int position) {
71 // 返回数据集中,当前position位置的数据的Id
72 return position;
73 }
74
75 @Override
76 public View getView(int position, View convertView, ViewGroup parent) {
77 // 返回当前position位置的视图
78 ImageView imageview;
79 if (convertView == null) {
80 // 通过数据上下文对象声明一个ImageView,并设置相关属性
81 imageview = new ImageView(context);
82 imageview.setImageResource(imagesIDs[position]);
83 imageview.setScaleType(ImageView.ScaleType.FIT_XY);
84 imageview.setLayoutParams(new Gallery.LayoutParams(150, 120));
85
86 } else {
87 imageview = (ImageView) convertView;
88 }
89 // 使用XML 中定义的样式为待显示的View设定背景样式
90 imageview.setBackgroundResource(itemBackground);
91
92 return imageview;
93 }
94
95 }
96
97 @Override
98 public boolean onCreateOptionsMenu(Menu menu) {
99 // Inflate the menu; this adds items to the action bar if it is present.
100 getMenuInflater().inflate(R.menu.main, menu);
101 return true;
102 }
103
104 }
 
 

in the code uses an XML file that defines the Android style, here simply to show pictures in the background for the Gallery style, is not necessary. Here is an XML resource file attrs.xml style code, files stored address is res / values ​​in.

 
  
1 <?xml version="1.0" encoding="utf-8"?> 
2 <resources>
3
4 <declare-styleable name="Gallery1">
5 <attr name="android:galleryItemBackground" />
6 </declare-styleable>
7
8 </resources>
 
 

results show:

 

 

source download

 

Summary

 

While Gallery in Android4.1 been deprecated after, but in the actual project, or have the opportunity to use this control, and through this control, explain the usage of BaseAdapter, BaseAdapter can define all custom return data, in actual projects are relatively common Adapter.

 

 

Please support the original, respect for the original, reproduced please indicate the source. Thank you.

没有评论:

发表评论