Android-Gallery[使用C# And Java实现]
2018-06-18 02:52:32来源:未知 阅读 ()
运行效果
C#实现
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<Gallery>(Resource.Id.gallery); _selectedImg = FindViewById<ImageView>(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实现
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
<RelativeLayout 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: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"> <ImageView android:layout_width="320dp" android:layout_height="320dp" android:id="@+id/currentImg" android:layout_centerHorizontal="true" /> <Gallery 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" /> </RelativeLayout>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ rand函数 2020-06-10
- C++ 在名称空间中使用using声明和using编译指令 2020-05-29
- 1.ffmpeg、ffplay、ffprobe命令使用 2020-05-11
- 关于使用ffmpeg的一些牢骚 2020-05-08
- Android P HIDL demo代码编写 (原创) 2020-05-07
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash