編輯:Android開發實例
Touch.java
- /**
- * 圖片浏覽、縮放、拖動、自動居中
- */
- public class Touch extends Activity implements OnTouchListener {
- Matrix matrix = new Matrix();
- Matrix savedMatrix = new Matrix();
- DisplayMetrics dm;
- ImageView imgView;
- Bitmap bitmap;
- float minScaleR;// 最小縮放比例
- static final float MAX_SCALE = 4f;// 最大縮放比例
- static final int NONE = 0;// 初始狀態
- static final int DRAG = 1;// 拖動
- static final int ZOOM = 2;// 縮放
- int mode = NONE;
- PointF prev = new PointF();
- PointF mid = new PointF();
- float dist = 1f;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.scale);
- imgView = (ImageView) findViewById(R.id.imag);// 獲取控件
- bitmap = BitmapFactory.decodeResource(getResources(), this.getIntent()
- .getExtras().getInt("IMG"));// 獲取圖片資源
- imgView.setImageBitmap(bitmap);// 填充控件
- imgView.setOnTouchListener(this);// 設置觸屏監聽
- dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);// 獲取分辨率
- minZoom();
- center();
- imgView.setImageMatrix(matrix);
- }
- /**
- * 觸屏監聽
- */
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction() & MotionEvent.ACTION_MASK) {
- // 主點按下
- case MotionEvent.ACTION_DOWN:
- savedMatrix.set(matrix);
- prev.set(event.getX(), event.getY());
- mode = DRAG;
- break;
- // 副點按下
- case MotionEvent.ACTION_POINTER_DOWN:
- dist = spacing(event);
- // 如果連續兩點距離大於10,則判定為多點模式
- if (spacing(event) > 10f) {
- savedMatrix.set(matrix);
- midPoint(mid, event);
- mode = ZOOM;
- }
- break;
- case MotionEvent.ACTION_UP:
- case MotionEvent.ACTION_POINTER_UP:
- mode = NONE;
- break;
- case MotionEvent.ACTION_MOVE:
- if (mode == DRAG) {
- matrix.set(savedMatrix);
- matrix.postTranslate(event.getX() - prev.x, event.getY()
- - prev.y);
- } else if (mode == ZOOM) {
- float newDist = spacing(event);
- if (newDist > 10f) {
- matrix.set(savedMatrix);
- float tScale = newDist / dist;
- matrix.postScale(tScale, tScale, mid.x, mid.y);
- }
- }
- break;
- }
- imgView.setImageMatrix(matrix);
- CheckView();
- return true;
- }
- /**
- * 限制最大最小縮放比例,自動居中
- */
- private void CheckView() {
- float p[] = new float[9];
- matrix.getValues(p);
- if (mode == ZOOM) {
- if (p[0] < minScaleR) {
- matrix.setScale(minScaleR, minScaleR);
- }
- if (p[0] > MAX_SCALE) {
- matrix.set(savedMatrix);
- }
- }
- center();
- }
- /**
- * 最小縮放比例,最大為100%
- */
- private void minZoom() {
- minScaleR = Math.min(
- (float) dm.widthPixels / (float) bitmap.getWidth(),
- (float) dm.heightPixels / (float) bitmap.getHeight());
- if (minScaleR < 1.0) {
- matrix.postScale(minScaleR, minScaleR);
- }
- }
- private void center() {
- center(true, true);
- }
- /**
- * 橫向、縱向居中
- */
- protected void center(boolean horizontal, boolean vertical) {
- Matrix m = new Matrix();
- m.set(matrix);
- RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
- m.mapRect(rect);
- float height = rect.height();
- float width = rect.width();
- float deltaX = 0, deltaY = 0;
- if (vertical) {
- // 圖片小於屏幕大小,則居中顯示。大於屏幕,上方留空則往上移,下方留空則往下移
- int screenHeight = dm.heightPixels;
- if (height < screenHeight) {
- deltaY = (screenHeight - height) / 2 - rect.top;
- } else if (rect.top > 0) {
- deltaY = -rect.top;
- } else if (rect.bottom < screenHeight) {
- deltaY = imgView.getHeight() - rect.bottom;
- }
- }
- if (horizontal) {
- int screenWidth = dm.widthPixels;
- if (width < screenWidth) {
- deltaX = (screenWidth - width) / 2 - rect.left;
- } else if (rect.left > 0) {
- deltaX = -rect.left;
- } else if (rect.right < screenWidth) {
- deltaX = screenWidth - rect.right;
- }
- }
- matrix.postTranslate(deltaX, deltaY);
- }
- /**
- * 兩點的距離
- */
- private float spacing(MotionEvent event) {
- float x = event.getX(0) - event.getX(1);
- float y = event.getY(0) - event.getY(1);
- return FloatMath.sqrt(x * x + y * y);
- }
- /**
- * 兩點的中點
- */
- private void midPoint(PointF point, MotionEvent event) {
- float x = event.getX(0) + event.getX(1);
- float y = event.getY(0) + event.getY(1);
- point.set(x / 2, y / 2);
- }
- }
scale.xml
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center" >
- <ImageView
- android:id="@+id/imag"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:scaleType="matrix" >
- </ImageView>
- </FrameLayout>
轉自:
策略模式其實特別簡單(聽到這句話,大家是不是心裡一下子放松了?)。比如排序,官方告訴大家我這裡有一個排序的接口ISort的sort()方法,然後民間各盡其能,實現
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用