Android album Effects
This article mainly introduces the Android album effect (implemented using C # and Java respectively). The original C # can also be used to develop apps ~ It feels good, because xiaobian does not like Java for the time being, so if you need it, you can refer
Running Effect
C # implementation
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 using Android. App;
Using Android. OS;
Using Android. Widget;
Namespace ImageDemo
{
[Activity (Label = "@ string/ApplicationName", MainLauncher = true, Icon = "@ drawable/icon")]
Public class MainActivity: Activity
{
Private Gallery _ gallery;
Private ImageView _ selectedImg;
Private readonly int [] _ imageIds = {
Resource. Drawable. test1,
Resource. Drawable. test2,
Resource. Drawable. test3,
Resource. Drawable. test4,
Resource. Drawable. test5,
Resource. Drawable. test6,
Resource. Drawable. test7,
Resource. Drawable. test8
};
Protected override void OnCreate (Bundle bundle)
{
Base. OnCreate (bundle );
SetContentView (Resource. Layout. Main );
_ Gallery = FindViewById (Resource. Id. gallery );
_ SelectedImg = FindViewById (Resource. Id. currentImg );
_ Gallery. Adapter = new ImageAdapter (this, _ imageIds );
_ Gallery. ItemSelected + = Gallery_ItemSelected;
}
Private void Gallery_ItemSelected (object sender, AdapterView. ItemSelectedEventArgs e)
{
_ SelectedImg. SetImageResource (_ imageIds [e. Position]);
}
}
Public class ImageAdapter: BaseAdapter
{
Private readonly Context _ context;
Private readonly int [] _ imageIds;
Public ImageAdapter (Context context, int [] imageIds)
{
_ Context = context;
_ ImageIds = imageIds;
}
Public override Object GetItem (int position)
{
Return null;
}
Public override long GetItemId (int position)
{
Return 0;
}
Public override int Count
{
Get {return _ imageIds. Length ;}
}
Public override View GetView (int position, View convertView, ViewGroup parent)
{
Var image = new ImageView (_ context );
Image. SetImageResource (_ imageIds [position]);
Image. LayoutParameters = new Gallery. LayoutParams (150,100 );
Image. SetScaleType (ImageView. ScaleType. FitXy );
Return image;
}
}
}
Java Implementation
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 package com. example. halower. gallerydemo;
Import android. content. Context;
Import android. OS. Bundle;
Import android. support. v7.app. ActionBarActivity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AdapterView;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Import static android. widget. Gallery. LayoutParams;
Public class MainActivity extends ActionBarActivity {
Private int [] imageIds = {
R. drawable. test1,
R. drawable. test2,
R. drawable. test3,
R. drawable. test4,
R. drawable. test5,
R. drawable. test6,
R. drawable. test7,
R. drawable. test8
};
Gallery gallery;
ImageView currentView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Gallery = (Gallery) findViewById (R. id. gallery );
ImageAdapter adapter = new ImageAdapter (this, imageIds );
CurrentView = (ImageView) findViewById (R. id. currentImg );
Gallery. setAdapter (adapter );
Gallery. setOnItemSelectedListener (new AdapterView. OnItemSelectedListener (){
@ Override
Public void onItemSelected (AdapterView parent, View view, int position, long id ){
CurrentView. setImageResource (imageIds [position]);
}
@ Override
Public void onNothingSelected (AdapterView parent ){
}
});
}
}
Class ImageAdapter extends BaseAdapter
{
Context _ context;
Int [] imageIds;
Public ImageAdapter (Context context, int [] imageIds ){
_ Context = context;
This. imageIds = imageIds;
}
@ Override
Public int getCount (){
Return imageIds. length;
}
@ Override
Public Object getItem (int position ){
Return null;
}
@ Override
Public long getItemId (int position ){
Return 0;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView imageView = new ImageView (_ context );
ImageView. setImageResource (imageIds [position]);
ImageView. setScaleType (ImageView. ScaleType. FIT_XY );
ImageView. setLayoutParams (new LayoutParams (70,100 ));
Return imageView;
}
}
Layout
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent"
Android: layout_height = "match_parent" android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Android: paddingBottom = "@ dimen/activity_vertical_margin" tools: context = ". MainActivity">
Android: layout_width = "320dp"
Android: layout_height = "320dp"
Android: id = "@ + id/currentImg"
Android: layout_centerHorizontal = "true"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: unselectedAlpha = "0.6"
Android: spacing = "2pt"
Android: layout_below = "@ + id/currentImg"
Android: id = "@ + id/gallery"/>