Gallery的使用之二

定义一个左右滑动的图片,文件图片是写死的 MainActivity

package com.lenovo.halo.gallery;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
//import android.os.LauncherSyncManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {

	private static final String TAG = "MyLauncher";
	private MyHandler m_h;
	private ImageView i;

	   private int[] ids = {
	  R.drawable.p12,
	 R.drawable.p13,
	 R.drawable.p1,
	 R.drawable.p2,
	 R.drawable.p3,
	 R.drawable.p4,
	 R.drawable.p5,
	 R.drawable.p6,
	 R.drawable.p7,
	 R.drawable.p8,
	R.drawable.p9,
	 R.drawable.p10,
	 R.drawable.p11
	// //no5test add img here!!!!!!!
	  };
   	int current = 0;
	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		i = (ImageView) findViewById(R.id.image);
 		 
     	i.setImageResource(ids[current]);
		PowerManager a = (PowerManager) getSystemService(Context.POWER_SERVICE);
		WakeLock w = a.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
		w.acquire();
		i.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				current++;
				if (current == ids.length) {
					current = 0;
				}
				m_h.removeMessages(1);
				Message message1 = new Message();
				message1.what = 1;
				m_h.sendMessage(message1);

				return false;
			}

		});
		m_h = new MyHandler();
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case 1:
				i.setImageResource(ids[current]);
				Log.e(TAG, "current img is" + ids[current] + "id is " + current);
				break;
			}
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		Log.e(TAG, "current img is" + ids[current] + "id is " + current
				+ "event is" + event.getAction());
		if (event.getAction() == KeyEvent.ACTION_UP) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_ENTER:
				current++;
				if (current == ids.length) {
					current = 0;
				}

				i.setImageResource(ids[current]);

			default:
				break;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onPause() {
		Log.i(TAG, "onPause");
		this.finish();
		System.exit(0);
		super.onPause();
	}
}

 定义XML文件

<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"
    tools:context="com.lenovo.halo.gallery.MainActivity" >

    
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/up_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/up"/>

    <ImageView
         android:id="@+id/down_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/down" />

</RelativeLayout>

 以上为直接读取drawable里边的文件图片写死的

 

你可能感兴趣的:(gallery)