English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Simple application example of Gallery drag and drop photo effect like Iphone in Android programming

This article example describes a simple application of Android programming imitating the iPhone drag photo effect Gallery. Share it with everyone for reference, as follows:

Step 1:Prepare image materials.

Set icon2,icon}}3,icon}}4,icon}}5,icon}}6five pictures imported to res/Add icon.png to the drawable folder, and there are a total of6picture.

Step 2:Create a new Android project named GalleryDemo.

Step 3:Design UI, modify the main.xml code as follows:

<&63;xml version="1.0" encoding="utf-8"&63;>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:background="@drawable/white"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <TextView
 android:id="@"+id/myTextView01"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 android:gravity="center_vertical|center_horizontal"
 />
 <Gallery
 android:id="@"+id/myGallery1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="bottom"
 />
</LinearLayout>

Step 4:The main program class GalleryDemo.java code is as follows:

package com.android.test;
import com.android.test.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryDemo extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 1)).setAdapter(new ImageAdapter(
  this));
 }
 public class ImageAdapter extends BaseAdapter {
 /* The class member myContext is the superclass of Context */
 private Context myContext;
 /* Using res/Drawable images as the source of image */
 private int[] myImageIds = { drawable.icon, drawable.icon2,
  drawable.icon3, drawable.icon4, drawable.icon5, drawable.icon6};
 /* The constructor has only one parameter, which is the Context to be stored */
 public ImageAdapter(Context c) {
  this.myContext = c;
 }
 /* Return the total number of all defined images */
 public int getCount() {
  return this.myImageIds.length;
 }
 /* Use the getItem method to get the current array ID of the image in the container */
 public Object getItem(int position) {
  return position;
 }
 public long getItemId(int position) {
  return position;
 }
 /* Get the current image View to be displayed, pass the array ID value to read and image */
 public View getView(int position, View convertView, ViewGroup parent) {
  /* Create an ImageView object */
  ImageView i = new ImageView(this.myContext);
  i.setImageResource(this.myImageIds[position]);
  i.setScaleType(ImageView.ScaleType.FIT_XY);
  /* Set the width and height of this ImageView object, the unit is dip */
  i.setLayoutParams(new Gallery.LayoutParams(120, 120));
  return i;
 }
 /* Use the getScale returned by the distance from the center to get the size of views (0.0f to 1.0f) */
 public float getScale(boolean focused, int offset) {
  /* Formula: 1 / (2 ^ offset) */
  return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
 }
 }
}

Step 5:Run it, the effect is as shown in the figure below:

 

Note: This code is mainly based on the Android SDK example code大全

Readers who are interested in more about Android-related content can check the special topics on this site: 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solutions', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', and 'Summary of Android Widget Usage'

I hope the description in this article will be helpful to everyone in Android program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, nor has it been edited by humans, nor does it assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like