編輯:Android開發實例
- package com.min.Test_Gallery;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.Matrix;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.GestureDetector;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.GestureDetector.OnGestureListener;
- import android.view.View.OnTouchListener;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class DisplayImage extends Activity implements OnTouchListener, OnGestureListener {
- private static final String TAG = "DisplayImage";
- private static final int FLING_MIN_DISTANCE = 100;
- private static final int FLING_MIN_VELOCITY = 200;
- /* 相關變量聲明 */
- private ImageView mImageView;
- private Button mButton01;
- private Button mButton02;
- private FrameLayout layout1;
- private LinearLayout layoutImage;
- private Bitmap bmp;
- private int id=0;
- private int displayWidth;
- private int displayHeight;
- private float scaleWidth=1;
- private float scaleHeight=1;
- private GestureDetector mGestureDetector;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /* 加載display.xml Layout */
- setContentView(R.layout.display);
- /* 取得屏幕分辨率大小 */
- DisplayMetrics dm=new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- displayWidth=dm.widthPixels;
- displayHeight=dm.heightPixels;
- /* 初始化相關變量 */
- Bundle bundle = this.getIntent().getExtras();
- Integer imageId = bundle.getInt("imageId");
- Log.i(TAG, "onCreate, imageId = " + imageId);
- bmp=BitmapFactory.decodeResource(getResources(), imageId);
- mImageView = (ImageView)findViewById(R.id.myImageView);
- mImageView.setImageBitmap(bmp);
- mImageView.setOnTouchListener(this);
- mImageView.setLongClickable(true);
- layout1 = (FrameLayout)findViewById(R.id.layout1);
- layoutImage = (LinearLayout)findViewById(R.id.layoutImage);
- mButton01 = (Button)findViewById(R.id.myButton1);
- mButton02 = (Button)findViewById(R.id.myButton2);
- /* 縮小按鈕onClickListener */
- mButton01.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- small();
- }
- });
- /* 放大按鈕onClickListener */
- mButton02.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- big();
- }
- });
- }
- // 用戶輕觸觸摸屏,由1個MotionEvent ACTION_DOWN觸發
- @Override
- public boolean onDown(MotionEvent e) {
- // TODO Auto-generated method stub
- // Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
- Log.i(TAG, "onDown...");
- return false;
- }
- /* 用戶按下觸摸屏、快速移動後松開,由1個MotionEvent ACTION_DOWN,
- * 多個ACTION_MOVE, 1個ACTION_UP觸發
- * 參數解釋:
- * e1:第1個ACTION_DOWN MotionEvent
- * e2:最後一個ACTION_MOVE MotionEvent
- * velocityX:X軸上的移動速度,像素/秒
- * velocityY:Y軸上的移動速度,像素/秒
- * 觸發條件 :
- * X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒
- * @see android.view.GestureDetector$OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
- */
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onFling...");
- if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
- // Fling left
- Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
- } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
- // Fling right
- Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- // 用戶長按觸摸屏,由多個MotionEvent ACTION_DOWN觸發
- @Override
- public void onLongPress(MotionEvent e) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onLongPress...");
- }
- // 用戶按下觸摸屏,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onScroll...");
- return false;
- }
- // 用戶輕觸觸摸屏,尚未松開或拖動,由一個1個MotionEvent ACTION_DOWN觸發
- // 注意和onDown()的區別,強調的是沒有松開或者拖動的狀態
- @Override
- public void onShowPress(MotionEvent e) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onShowPress...");
- }
- // 用戶(輕觸觸摸屏後)松開,由一個1個MotionEvent ACTION_UP觸發
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onSingleTapUp...");
- return false;
- }
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onTouch...");
- // Set button visible
- mButton01.setVisibility(View.VISIBLE);
- mButton02.setVisibility(View.VISIBLE);
- return mGestureDetector.onTouchEvent(event);
- }
- // @Override
- // public boolean onTouchEvent(MotionEvent event) {
- // // TODO Auto-generated method stub
- // super.onTouchEvent(event);
- //
- // Log.i(TAG, "onTouchEvent");
- // // Set button visible
- // mButton01.setVisibility(View.VISIBLE);
- // mButton02.setVisibility(View.VISIBLE);
- //
- // return true;
- // }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- super.onKeyDown(keyCode, event);
- Log.i(TAG, "onKeyDown...");
- // Set button visible
- mButton01.setVisibility(View.VISIBLE);
- mButton02.setVisibility(View.VISIBLE);
- return true;
- }
- /* 圖片縮小的method */
- private void small() {
- int bmpWidth=bmp.getWidth();
- int bmpHeight=bmp.getHeight();
- Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
- /* 設置圖片縮小的比例 */
- double scale=0.8;
- /* 計算出這次要縮小的比例 */
- scaleWidth=(float) (scaleWidth*scale);
- scaleHeight=(float) (scaleHeight*scale);
- /* 產生reSize後的Bitmap對象 */
- Matrix matrix = new Matrix();
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
- bmpHeight,matrix,true);
- if(id==0) {
- /* 如果是第一次按,就刪除原來默認的ImageView */
- layoutImage.removeView(mImageView);
- } else {
- /* 如果不是第一次按,就刪除上次放大縮小所產生的ImageView */
- layoutImage.removeView((ImageView)findViewById(id));
- }
- /* 產生新的ImageView,放入reSize的Bitmap對象,再放入Layout中 */
- id++;
- ImageView imageView = new ImageView(this);
- imageView.setId(id);
- imageView.setImageBitmap(resizeBmp);
- layoutImage.addView(imageView);
- Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()
- + ", imageView.getHeight() = " + imageView.getHeight());
- setContentView(layout1);
- /* 因為圖片放到最大時放大按鈕會disable,所以在縮小時把它重設為enable */
- mButton02.setEnabled(true);
- mButton02.setTextColor(Color.MAGENTA);
- }
- /* 圖片放大的method */
- private void big() {
- int bmpWidth=bmp.getWidth();
- int bmpHeight=bmp.getHeight();
- Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
- /* 設置圖片放大的比例 */
- double scale=1.25;
- /* 計算這次要放大的比例 */
- scaleWidth=(float)(scaleWidth*scale);
- scaleHeight=(float)(scaleHeight*scale);
- /* 產生reSize後的Bitmap對象 */
- Matrix matrix = new Matrix();
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
- bmpHeight,matrix,true);
- if(id==0) {
- /* 如果是第一次按,就刪除原來設置的ImageView */
- layoutImage.removeView(mImageView);
- } else {
- /* 如果不是第一次按,就刪除上次放大縮小所產生的ImageView */
- layoutImage.removeView((ImageView)findViewById(id));
- }
- /* 產生新的ImageView,放入reSize的Bitmap對象,再放入Layout中 */
- id++;
- ImageView imageView = new ImageView(this);
- imageView.setId(id);
- imageView.setImageBitmap(resizeBmp);
- layoutImage.addView(imageView);
- setContentView(layout1);
- /* 如果再放大會超過屏幕大小,就把Button disable */
- if( scaleWidth * scale * bmpWidth > bmpWidth * 3 ||
- scaleHeight * scale * bmpHeight > bmpWidth * 3 ||
- scaleWidth * scale * bmpWidth > displayWidth * 5 ||
- scaleHeight * scale * bmpHeight > displayHeight * 5) {
- mButton02.setEnabled(false);
- mButton02.setTextColor(Color.GRAY);
- } else {
- mButton02.setEnabled(true);
- mButton02.setTextColor(Color.MAGENTA);
- }
- }
- }
display.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/layout1"
- >
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="19"
- android:scrollbars="vertical"
- android:fadingEdge="vertical">
- <HorizontalScrollView
- android:layout_height="fill_parent"
- android:layout_width="wrap_content">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:id="@+id/layoutImage"
- >
- <ImageView
- android:id="@+id/myImageView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="19"
- android:paddingTop="5dip"
- android:paddingBottom="5dip"
- />
- </LinearLayout>
- </HorizontalScrollView >
- </ScrollView>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/myButton1"
- android:layout_width="45dip"
- android:layout_height="30dip"
- android:layout_alignParentLeft="true"
- android:gravity="left"
- style="@style/my_style_button"
- android:visibility="gone"
- android:text="縮小"
- />
- <Button
- android:id="@+id/myButton2"
- android:layout_width="45dip"
- android:layout_height="30dip"
- android:layout_alignParentRight="true"
- android:gravity="right"
- style="@style/my_style_button"
- android:visibility="gone"
- android:text="放大"
- />
- </RelativeLayout>
- </FrameLayout>
本文使用Matrix實現Android實現圖片縮放與旋轉。示例代碼如下:代碼如下:package com.android.matrix;import androi
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用