編輯:關於 Android 程式設計
最省事的方式是發一個 ACTION_PICK 意圖,把選擇工作交給系統相簿:
package com.example.testphotoedit;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView chosenImageView, copyPicture;
private Button choosePicture;
private Uri imageFileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
choosePicture = (Button) findViewById(R.id.button_chose);
chosenImageView = (ImageView) findViewById(R.id.chose_picture);
copyPicture = (ImageView) findViewById(R.id.copy_picture);
choosePicture.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button_chose) {
// 啟動相簿應用程式
Intent chooseIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(chooseIntent, 0);
}
}
}
挑到圖之後,最大的風險是記憶體。這裡示範一個重要技巧:先只讀取圖片的邊界資訊,據以計算縮放倍率,再正式解碼。
/* 在返回的意圖資料中取得所選圖片的 URI */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
imageFileUri = data.getData();
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth() / 2 - 100;
int dh = currentDisplay.getHeight() / 2 - 100;
try {
BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
// 第一次:只載入尺寸,不載入圖片本身
bmpFactory.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeStream(
getContentResolver().openInputStream(imageFileUri),
null, bmpFactory);
int heightRatio = (int) Math.ceil(bmpFactory.outHeight / (float) dh);
int widthRatio = (int) Math.ceil(bmpFactory.outWidth / (float) dw);
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactory.inSampleSize = heightRatio;
} else {
bmpFactory.inSampleSize = widthRatio;
}
}
// 第二次:正式載入圖片
bmpFactory.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(
getContentResolver().openInputStream(imageFileUri),
null, bmpFactory);
chosenImageView.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
這裡取 heightRatio 與 widthRatio 中的較大值作為 inSampleSize,確保縮完之後兩個方向都還能塞進目標框內——代價是圖片可能略小於理想尺寸,但绝不会超出可用區域。這是記憶體與畫質之間的合理取捨。
複製一張圖最直觀的做法:建立一張同尺寸的新 Bitmap,用 Canvas 把原圖畫上去。
Bitmap alteredBitmap = Bitmap.createBitmap(
bmp.getWidth(), bmp.getWidth(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
canvas.drawBitmap(bmp, 0, 0, paint);
copyPicture.setImageBitmap(alteredBitmap);
這裡有個小疏漏值得指出:第三行建立新 Bitmap 時寬度寫了 bmp.getWidth() 兩次,高度也應該用 bmp.getHeight(),否則非正方形的圖會被拉伸變形。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testphotoedit.MainActivity$PlaceholderFragment">
<Button
android:id="@+id/button_chose"
android:layout_width="120dp"
android:layout_height="60dp"
android:text="選擇圖片" />
<ImageView
android:id="@+id/chose_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_chose" />
<ImageView
android:id="@+id/copy_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chose_picture" />
</RelativeLayout>
另外提醒兩點:使用 ACTION_PICK 時通常不需額外權限,但若要讀取外部儲存中的任意檔案,仍應視 Android 版本補上讀取權限;而 getDefaultDisplay() 在 API 30 之後已不建議使用,改以 WindowMetrics 取得螢幕尺寸較為恰當。
Android 數據庫高手秘籍(三)——用 LitePal 輕鬆完成資料表升級
上一篇我們把 LitePal 的基本用法跑通了,也體會到了「不用親手寫建表語句」的暢快。但建表只是資料庫生命週期的起點。需求一旦變動,當初設計好的表結構幾乎必然要動刀,所
Android 仿京東首頁的輪播文字效果
京東首頁中常見一種文字輪播效果:前方是固定前綴,後方是會滾動的內容,並且可以點擊跳轉。本文說明如何以自訂 TextView 實作類似效果。 本次重點是後方滾動的文字,前
Android 動畫系統之屬性動畫(Property Animation)完全解析
為什麼需要屬性動畫 Android 3.0 引入的屬性動畫系統,是對早期 View Animation(補間動畫)體系的一次徹底升級,主要體現在四個方面: 彌補了 Vi
Android 頁面間回傳結果:startActivityForResult 與 setResult 實作
頁面跳轉不只是「過去」,很多時候還需要「回來時帶答案」——例如從清單頁挑一個使用者,回到表單頁帶入。這就必須用到 startActivityForResult 與 set