欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-10-28 20:01:10
运行效果 c#实现 using android.app; using android.os; using android.widget; name...

运行效果

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

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>