返回顶部
首页 > 资讯 > 移动开发 >Android相册效果(使用C#和Java分别实现)
  • 943
分享到

Android相册效果(使用C#和Java分别实现)

C#JAVAAndroid 2022-06-06 09:06:16 943人浏览 八月长安
摘要

运行效果 C#实现 using Android.App; using Android.OS; using Android.Widget; namespace ImageDe

运行效果

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实现tcp断点上传 后台C#服务接收快速解决C# android base-64 字符数组的无效长度问题C#之Android手机App开发C#开发Android百度地图手机应用程序(多地图展示)Android仿微信菜单(Menu)(使用C#和Java分别实现)Android下拉列表(Spinner)效果(使用C#和Java分别实现)C#采用winform实现类似Android的Listener详解C#开发Android应用程序的流程


--结束END--

本文标题: Android相册效果(使用C#和Java分别实现)

本文链接: https://lsjlt.com/news/26672.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作