編輯:Android 開發實例
我昨天花了點時間重新整理並補上大量註解,希望大家都能看得很明白。整理後在原本的基礎上,實作了一個具備 3D 特效的程式管理功能,讓它變得更實用,而不只是單純顯示圖片。
最終效果:以 3D 方式展示所有已安裝的程式列表,點擊某張圖示時會動態放大移到最前方,長按則可開啟該程式。
整體流程大致如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 取得應用程式資訊列表(含圖示)
getLauncher();
final CoverFlow cf = new CoverFlow(this); // 改寫 getChildStaticTransformation,產生層疊與放大效果
cf.setAdapter(new ImageAdapter(this));
// 自訂圖片填充方式
ImageAdapter imageAdapter = new ImageAdapter(this);
cf.setAdapter(imageAdapter);
cf.setAnimationDuration(1500);
cf.setOnItemClickListener(this);
cf.setOnItemLongClickListener(lonClick);
setContentView(cf);
}
package com.android.tank;
import android.content.ComponentName;
import android.graphics.drawable.Drawable;
public class LauncherItem {
Drawable icon;
String name;
ComponentName component;
LauncherItem(Drawable d, String s, ComponentName cn) {
icon = d;
name = s;
component = cn;
}
public Drawable getIcon() { return icon; }
public void setIcon(Drawable icon) { this.icon = icon; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public ComponentName getComponent() { return component; }
public void setComponent(ComponentName component) { this.component = component; }
}
// 保存從應用程式取得的資訊列表(含圖示);你也可以自訂圖片集合
List<LauncherItem> lvalue;
// 取得 app 列表資訊
public void getLauncher() {
lvalue = new ArrayList<LauncherItem>();
PackageManager pkgMgt = this.getPackageManager(); // 關鍵方法
// 查詢所有 launcher 並載入 List
Intent it = new Intent(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
for (int i = 0; i < ra.size(); i++) {
ActivityInfo ai = ra.get(i).activityInfo;
Drawable icon = ai.loadIcon(pkgMgt);
String label = ai.loadLabel(pkgMgt).toString();
ComponentName c = new ComponentName(ai.applicationInfo.packageName, ai.name);
LauncherItem item = new LauncherItem(icon, label, c);
lvalue.add(item);
}
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
public int getCount() {
return resIds.length;
}
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(mContext);
iv.setImageDrawable(lvalue.get(position).icon);
// 加入處理過(含倒影)的圖片
iv.setImageBitmap(MyImgView.createReflectedImage(MyImgView
.drawableToBitmap(lvalue.get(position).icon)));
iv.setLayoutParams(new Gallery.LayoutParams(80, 60));
return iv;
}
}
package com.android.tank;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
public class MyImgView {
/**
* 加入倒影:先翻轉圖片,再由上到下逐漸提高透明度
*/
public static Bitmap createReflectedImage(Bitmap originalImage) {
final int reflectionGap = 4;
int width = originalImage.getWidth();
int height = originalImage.getHeight();
// 沿 Y 軸翻轉(不縮放)
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
// 只取圖片下半部並套用翻轉矩陣
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);
// 建立一個更高、可容納倒影的新 Bitmap
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(originalImage, 0, 0, null);
// 畫出間隔
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
// 畫出倒影
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
// 以線性漸層覆蓋倒影,營造淡出效果
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
// 把 Drawable 轉成 Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
}
package com.android.tank;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
// 自訂的 Gallery
public class CoverFlow extends Gallery {
private Camera mCamera = new Camera();
private int mMaxRotationAngle = 50;
private int mMaxZoom = -500;
private int mCoveflowCenter;
private boolean mAlphaMode = true;
private boolean mCircleMode = false;
public CoverFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}
public CoverFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
public int getMaxRotationAngle() { return mMaxRotationAngle; }
public void setMaxRotationAngle(int maxRotationAngle) { mMaxRotationAngle = maxRotationAngle; }
public boolean getCircleMode() { return mCircleMode; }
public void setCircleMode(boolean isCircle) { mCircleMode = isCircle; }
public boolean getAlphaMode() { return mAlphaMode; }
public void setAlphaMode(boolean isAlpha) { mAlphaMode = isAlpha; }
public int getMaxZoom() { return mMaxZoom; }
public void setMaxZoom(int maxZoom) { mMaxZoom = maxZoom; }
private int getCenterOfCoverflow() {
return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
}
private static int getCenterOfView(View view) {
return view.getLeft() + view.getWidth() / 2;
}
// 改寫 Gallery 方法,產生層疊與放大效果
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
if (childCenter == mCoveflowCenter) {
transformImageBitmap((ImageView) child, t, 0, 0);
} else {
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
}
transformImageBitmap((ImageView) child, t, rotationAngle,
(int) Math.floor((mCoveflowCenter - childCenter) / (childWidth == 0 ? 1 : childWidth)));
}
return true;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
/**
* 依傳入的角度轉換 ImageView 的 Bitmap
*/
private void transformImageBitmap(ImageView child, Transformation t,
int rotationAngle, int d) {
mCamera.save();
final Matrix imageMatrix = t.getMatrix();
final int imageHeight = child.getLayoutParams().height;
final int imageWidth = child.getLayoutParams().width;
final int rotation = Math.abs(rotationAngle);
mCamera.translate(0.0f, 0.0f, 100.0f);
// 角度越小,放大越多
if (rotation <= mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
mCamera.translate(0.0f, 0.0f, zoomAmount);
if (mCircleMode) {
if (rotation < 40)
mCamera.translate(0.0f, 155, 0.0f);
else
mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);
}
if (mAlphaMode) {
((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));
}
}
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
mCamera.restore();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="30dp" />
<ImageSwitcher android:id="@+id/imageswitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
以上就是大致內容了,整理了這麼多東西貼出來花了我不少時間,希望對大家有幫助!原始碼下載位址:MyApplicationMenu.rar(請尊重作者 tank 的成果)。
Android MediaPlayer 教學:動手做一個音樂播放器
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而最最直接的一條路就是 MediaPlayer 類別。它被封裝在 android.media 套件中,可以
Android MediaPlayer:打造自己的音樂播放器
Android 在音訊與影片播放上提供了相當完整的支援,而其中最常被直接使用的一條路徑,就是 MediaPlayer 這個類別。它被封裝在 android.media 套
Android 對話框彈出位置與透明度的實作詳解
預設的對話框總是顯示在畫面正中央,但實際開發中,我們常常需要調整它的位置(例如置於螢幕上方或下方),或是做出透明、半透明乃至背景模糊的效果。要達成這些客製化需求,關鍵在於
Android 音頻錄製與音頻視覺化開發案例
Android 提供多種音頻處理方式,從呼叫系統內建錄音程式,到自行使用 MediaRecorder、AudioRecord 進行即時聲波分析,都能延伸出不同的應用。本文