Android相册效果(使用C#和Java分别实现)
作者:junjie 时间:2022-08-21 15:19:10
运行效果
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>
标签:Android,相册,C#,Java
0
投稿
猜你喜欢
java实现的n*n矩阵求值及求逆矩阵算法示例
2022-07-19 06:45:06
Java SpringBoot Validation用法案例详解
2023-10-21 15:06:49
c#多线程的应用全面解析
2023-03-03 21:15:02
Android中CountDownTimer 实现倒计时功能
2022-01-03 00:00:24
详解MyBatis配置typeAliases的方法
2023-11-29 06:21:52
winform把Office转成PDF文件
2023-03-29 01:09:32
springboot中使用redis并且执行调试lua脚本
2022-02-15 08:49:52
Java调取创蓝253短信验证码的实现代码
2021-11-05 00:48:10
Android中ImageCropper矩形、圆形 裁剪框的实现方法
2022-04-17 02:41:28
Android开发实现Files文件读取解析功能示例
2021-06-15 04:56:37
Java8 Lambda表达式详解及实例
2023-02-06 20:59:15
基于App自适应draw9patch不失真背景的方法详解
2022-04-06 17:36:51
实例解析Json反序列化之ObjectMapper(自定义实现反序列化方法)
2023-11-23 18:07:25
Win10下C# DateTime出现星期几问题的解决方法
2023-01-12 16:42:04
Android仿英语流利说取词放大控件的实现方法(附demo源码下载)
2022-07-10 23:14:18
mybatis源码解读-Java中executor包的语句处理功能
2023-09-03 06:34:16
基于Java实现的图的广度优先遍历算法
2021-06-02 06:51:20
SpringBoot接口加密解密统一处理
2023-04-12 19:42:04
Spring boot集成redis lettuce代码实例
2022-08-03 12:21:19
c#数据的序列化和反序列化(推荐版)
2022-03-27 22:55:56