編輯:關於android開發
代碼如下:
java:
package snowfox.android;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ImageView v1;
private int widthOrg, heightOrg, w;
float scale = 1;
float scaleWidth, scaleHeight;
Bitmap OrgMap, newMap;
BitmapDrawable bmd;
Matrix matrix = new Matrix();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v1 = (ImageView)findViewById(R.id.image01);
OrgMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
widthOrg = OrgMap.getWidth();
heightOrg = OrgMap.getHeight();
//確定圖片初始縮放比例,一般以適應屏幕寬度為准
w = 320;
scale = (float)w/widthOrg;
matrix = new Matrix();
matrix.postScale(scale, scale);
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//放大圖片
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
//設置放大尺寸,若大於2倍,則停止放大,另外,小於1200是為了防止因圖片太大造成內存洩露
if(scale < 2.0 && w < 1200)
{
scale += 0.1;
matrix.reset(); //重置矩陣
matrix.postScale(scale, scale); //設置矩陣屬性
//重新繪圖
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
//轉化為drawable圖像,使其可以顯示在imageview中
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " +h);
}
return true;
}
//縮小圖片
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
//設置縮放尺寸,若小於0.1倍,則停止縮小
if(scale > 0.1)
{
scale -= 0.05;
matrix.reset(); //重置矩陣
matrix.postScale(scale, scale); //設置矩陣屬性
//重新繪圖
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
//轉化為drawable圖像,使其可以顯示在imageview中
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " +h);
}
return true;
}
else
return super.onKeyDown(keyCode, event); //在其他情況下才調用super是為了屏蔽音量按鍵原來的功能,同時保證其他按鍵原功能可用
}
}
xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:fillViewport = "true">
<LinearLayout
android:gravity ="center"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:id = "@+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType ="fitCenter"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
用Kotlin創建第一個Android項目(KAD 01),kotlinandroid原文標題:Create your first Android project usi
手機影音3--本地視頻列表,影音3--列表 1.寫布局 相對布局 : ListView和TextView和ProgressBar,初始化 1 <?xml ve
android 網絡_網絡源碼查看器,android源碼xml設計 <?xml version=1.0?> -<LinearLayout tools:
Android—Service與Activity的交互,androidactivity service—Android的四大組件之一。人稱“後台服