萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android相冊效果

Android相冊效果

   這篇文章主要介紹了Android相冊效果(使用C#和Java分別實現),原來C#也可以開發APP,小編第一次見了~感覺不錯,因為小編暫時不喜歡Java,所以,需要的朋友可以參考下

  運行效果

  C#實現

  ?

  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

  69using 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實現

  ?

  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

  85package 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

copyright © 萬盛學電腦網 all rights reserved