編輯:關於Android編程
package com.example.mpaint; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.ContentUris; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnSeekBarChangeListener,OnClickListener { private ImageView iv_change; private SeekBar sb_alpha, sb_red, sb_green, sb_blue; private TextView tv_dispaly; private float a = 0f, r = 0f, g = 0f, b = 0f; private String Path; private Button btn_photo, btn_picture, btn_save, btn_origin, btn_delete, btn_see; private Bitmap photo = null, photoorigin; private File mCurrentPhotoFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 新建圖片存儲的目錄 Path = Environment.getExternalStorageDirectory().toString() + "/mFilter"; File file = new File(Path); if (!file.exists()) { file.mkdir(); } iv_change = (ImageView) findViewById(R.id.iv_change); tv_dispaly = (TextView) findViewById(R.id.tv_display); tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + +" " + "b: " + String.valueOf((b))); // 從資源文件中獲取圖片 photoorigin = BitmapFactory.decodeResource(getResources(), R.drawable.origin); // 初始化進度條 initSeek(); // 初始化按鈕 initButton(); } private void initButton() { btn_save = (Button) findViewById(R.id.btn_save); btn_save.setOnClickListener(this); btn_photo = (Button) findViewById(R.id.btn_photo); btn_photo.setOnClickListener(this); btn_picture = (Button) findViewById(R.id.btn_picture); btn_picture.setOnClickListener(this); btn_origin = (Button) findViewById(R.id.btn_origin); btn_origin.setOnClickListener(this); btn_delete = (Button) findViewById(R.id.btn_delete); btn_delete.setOnClickListener(this); btn_see = (Button) findViewById(R.id.btn_see); btn_see.setOnClickListener(this); } private void initSeek() { sb_alpha = (SeekBar) findViewById(R.id.sb_alpha); sb_alpha.setOnSeekBarChangeListener(this); sb_red = (SeekBar) findViewById(R.id.sb_red); sb_red.setOnSeekBarChangeListener(this); // 設置進度條默認位置 sb_red.setProgress(50); sb_green = (SeekBar) findViewById(R.id.sb_green); sb_green.setOnSeekBarChangeListener(this); sb_green.setProgress(50); sb_blue = (SeekBar) findViewById(R.id.sb_blue); sb_blue.setOnSeekBarChangeListener(this); sb_blue.setProgress(50); } // 重寫OnSeekBarChangeListener @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch (seekBar.getId()) { case R.id.sb_alpha: a = (float) (progress / 100.0); break; case R.id.sb_red: r = (float) ((progress - 50) / 5.0); break; case R.id.sb_green: g = (float) ((progress - 50) / 5.0); break; case R.id.sb_blue: b = (float) ((progress - 50) / 5.0); break; default: break; } setArgb(1 - a, r + 1, g + 1, b + 1); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } public void setArgb(float alpha, float red, float green, float blue) { tv_dispaly.setText("a: " + String.valueOf((a * 100)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + " " + "b: " + String.valueOf((b))); // 新建顏色矩陣 ColorMatrix mColorMatrix = new ColorMatrix(new float[] { red, 0, 0, 0, 0, 0, green, 0, 0, 0, 0, 0, blue, 0, 0, 0, 0, 0, alpha, 0 }); // 使用顏色矩陣過濾圖片 iv_change.getDrawable().setColorFilter( new ColorMatrixColorFilter(mColorMatrix)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_origin: a = 0f; r = 0f; g = 0f; b = 0f; tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + " " + "b: " + String.valueOf((b))); sb_alpha.setProgress(0); sb_red.setProgress(50); sb_green.setProgress(50); sb_blue.setProgress(50); // 初始化圖片 if (photo != null) { iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } else { iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photoorigin); } break; case R.id.btn_save: saveImageView(); break; case R.id.btn_photo: takephoto(); break; case R.id.btn_picture: takepicture(); break; case R.id.btn_see: see(); break; case R.id.btn_delete: new AlertDialog.Builder(MainActivity.this) .setTitle("提示信息") .setMessage("是否刪除路徑" + Path + "下的所有文件" + "?") .setNegativeButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { delete(); dialog.dismiss(); // 判斷是否完全刪除 File file = new File(Path); File[] childFile = file.listFiles(); if (childFile.length == 0) { Toast.makeText(MainActivity.this, "刪除成功", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(MainActivity.this, "刪除失敗", Toast.LENGTH_LONG) .show(); } } }) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); break; default: break; } } // 查看文件 private void see() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); File file = new File(Path); intent.setDataAndType(Uri.fromFile(file), "*/*"); startActivityForResult(intent, 2); } private void delete() { File file = new File(Path); File[] childFile = file.listFiles(); for (File file2 : childFile) { file2.delete(); } } private void takepicture() { // 相冊 Intent intentPick = new Intent(Intent.ACTION_PICK, null); intentPick.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intentPick, 0); } private void takephoto() { // 照相 mCurrentPhotoFile = new File(Path, "temp.jpg"); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile)); startActivityForResult(intent, 1); } private void saveImageView() { // 自定義文件名 final String path = Path + "/" + getNowDateTime("yyyyMMddHHmmss") + ".jpg"; new AlertDialog.Builder(MainActivity.this) .setTitle("提示信息") .setMessage("是否將文件保存到" + Path + "?") .setNegativeButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 設置view可緩存.然後以bitmap獲取view的緩存 iv_change.setDrawingCacheEnabled(true); Bitmap photofinally = iv_change.getDrawingCache(); try { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path)); // 將bitmap壓縮存儲,100表示不壓縮 photofinally.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); bos = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } iv_change.setDrawingCacheEnabled(false); File file = new File(path); dialog.dismiss(); if (file.exists()) { Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "保存失敗", Toast.LENGTH_LONG).show(); } } }) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); } public static String getNowDateTime(String format) { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(format);// "yyyyMMdd" String dateString = formatter.format(currentTime); return dateString; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 0: if (data != null) { // 相冊uri獲取 Uri uri = data.getData(); // 共享相機獲取的數據 ContentResolver cr = this.getContentResolver(); try { // 回收bitmap if (photo != null && !photo.isRecycled()) { photo.recycle(); System.gc(); } // 將好;獲取的數據編碼成bitmap photo = BitmapFactory.decodeStream(cr.openInputStream(uri)); } catch (Exception e) { e.printStackTrace(); } iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } break; case 1: // resultCode=-1為成功 if (resultCode == -1) { ContentResolver cr = getContentResolver(); Uri fileUri = Uri.fromFile(mCurrentPhotoFile); // 刷新單個文件 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri)); // 刷新sd卡 // sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, // dirUri)); try { if (photo != null && !photo.isRecycled()) { photo.recycle(); System.gc(); } // 通過uri讀取資源 photo = BitmapFactory.decodeStream(cr .openInputStream(fileUri)); } catch (Exception e) { e.printStackTrace(); } iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } break; case 2: break; default: break; } super.onActivityResult(requestCode, resultCode, data); } }
private void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 設置裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 320); intent.putExtra("outputY", 320); //返回數據 intent.putExtra("return-data", true); startActivityForResult(intent, REQUEST_CODE_HEAD); } //處理onActivityResult private Bitmap saveImageView(String path, Intent data) throws Exception { Bitmap photo = null; //獲取綁定數據包 Bundle extras = data.getExtras(); if (extras != null) { //獲取綁定數據 photo = extras.getParcelable("data"); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path)); photo.compress(Bitmap.CompressFormat.JPEG, 60, bos); bos.flush(); bos.close(); bos = null; } return photo; }
官網地址詳解分析:http://developer.android.com/guide/components/aidl.html一)什麼是AIDL –》 應用
app的啟動方式: 1.)冷啟動 當啟動應用時,後台沒有該應用的進程,這時系統會重新創建一個新的進程分配給該應用,這個啟動方式就是冷啟動。冷啟動因為系
因為Android的編譯系統不同於Linux Kernel的遞歸式的編譯系統,它的編譯系統是一種稱之為independent的模式,每個模塊基本獨立(它有可能依賴其他模塊
所謂SAX,就是SAX simple API for XML,是一個解析XML文件的簡單API。SAX解析XML的工作原理,是對文檔進行順序掃描,當掃描到文檔開始與結束,