編輯:關於Android編程
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Options opts = new Options();
//只請求圖片寬高,不解析圖片像素
opts.inJustDecodeBounds = true;
//返回null,獲取圖片寬高,保存在opts對象中
BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
//獲取圖片寬高
int imageWidth = opts.outWidth;
int imageHeight = opts.outHeight;
//獲取屏幕寬高
Display dp = getWindowManager().getDefaultDisplay();
int screenWidth = dp.getWidth();
int screenHeight = dp.getHeight();
//計算縮放比例
int scale = 1;
int scaleWidth = imageWidth / screenWidth;
int scaleHeight = imageHeight / screenHeight;
//判斷取哪個比例
if(scaleWidth >= scaleHeight && scaleWidth > 1){
scale = scaleWidth;
}
else if(scaleWidth < scaleHeight && scaleHeight > 1){
scale = scaleHeight;
}
//設置縮小比例
opts.inSampleSize = scale;
opts.inJustDecodeBounds = false;
//獲取縮小後的圖片的像素信息
Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bm);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加載原圖
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.photo3);
//創建副本
//1.創建與原圖一模一樣大小的bitmap對象,該對象中目前是沒有內容的,可以比喻為創建了和原圖一樣大小的白紙
Bitmap bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//2.創建畫筆對象
Paint paint = new Paint();
//3.創建畫板,把白紙鋪到畫板上
Canvas canvas = new Canvas(bmCopy);
//4.開始作畫
canvas.drawBitmap(bmSrc, new Matrix(), paint);
ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
iv_src.setImageBitmap(bmSrc);
ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
iv_copy.setImageBitmap(bmCopy);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加載原圖
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.photo3);
//創建副本
//1.創建與原圖一模一樣大小的bitmap對象,該對象中目前是沒有內容的,可以比喻為創建了和原圖一樣大小的白紙
Bitmap bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//2.創建畫筆對象
Paint paint = new Paint();
//3.創建畫板,把白紙鋪到畫板上
Canvas canvas = new Canvas(bmCopy);
Matrix mt = new Matrix();
//平移效果,指定平移距離
// mt.setTranslate(20, 10);
//縮放效果,指定縮放比例
// mt.setScale(2, 0.5f, bmCopy.getWidth() / 2, bmCopy.getHeight() / 2);
//旋轉效果
// mt.setRotate(45, bmCopy.getWidth() / 2, bmCopy.getHeight() / 2);
//鏡面效果
// mt.setScale(-1, 1);
// mt.postTranslate(bmCopy.getWidth(), 0);
//倒影效果
mt.setScale(1, -1);
mt.postTranslate(0, bmCopy.getHeight());
canvas.drawBitmap(bmSrc, mt, paint);
ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
iv_src.setImageBitmap(bmSrc);
ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
iv_copy.setImageBitmap(bmCopy);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
//白紙
bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//筆
paint = new Paint();
//畫板
canvas = new Canvas(bmCopy);
//作畫
canvas.drawBitmap(bmSrc, new Matrix(), paint);
iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmCopy);
iv.setOnTouchListener(new OnTouchListener() {
//用戶手指只要觸摸屏幕,就會產生觸摸事件
@Override
public boolean onTouch(View v, MotionEvent event) {
//判斷觸摸事件的類型
switch (event.getAction()) {
//手指觸摸
case MotionEvent.ACTION_DOWN:
//觸摸事件中會包含手指觸摸的坐標
startX = (int) event.getX();
startY = (int) event.getY();
break;
//手指滑動
case MotionEvent.ACTION_MOVE:
int newX = (int) event.getX();
int newY = (int) event.getY();
//指定線的起點和終點
canvas.drawLine(startX, startY, newX, newY, paint);
iv.setImageBitmap(bmCopy);
//把本此畫線的終點設置為下一次畫線的起點
startX = newX;
startY = newY;
break;
//手指抬起
case MotionEvent.ACTION_UP:
break;
}
//true表示告訴系統,這個觸摸事件由iv處理
//false表示不處理該觸摸事件,事件往上傳遞
return true;
}
});
}
public void red(View v){
paint.setColor(Color.RED);
}
public void green(View v){
paint.setColor(Color.GREEN);
}
public void brush(View v){
//改變線條粗細
paint.setStrokeWidth(8);
}
public void save(View v){
File file = new File("sdcard/dazuo.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
//把圖片壓縮到本地文件
bmCopy.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//手動發一個sd卡就緒廣播
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加載圖片
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.big2);
final Bitmap bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(bmCopy);
canvas.drawBitmap(bmSrc, new Matrix(), paint);
final ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmCopy);
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
//把該坐標對應的像素,置為透明色
for (int i = -5; i < 5; i++) {
for (int j = -5; j < 5; j++) {
if(Math.sqrt(i*i + j*j) <= 5){
if(x + i < bmCopy.getWidth() && y + j < bmCopy.getHeight()
&& x + i >= 0 && y + j >= 0)
//把指定坐標的像素置為指定的顏色
bmCopy.setPixel(x + i, y + j, Color.TRANSPARENT);
}
}
}
iv.setImageBitmap(bmCopy);
break;
}
return true;
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView sv = (SurfaceView) findViewById(R.id.sv);
//獲取surfaceview的控制器
final SurfaceHolder holder = sv.getHolder();
// Thread t = new Thread(){
// @Override
// public void run() {
// try {
// sleep(200);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// MediaPlayer player = new MediaPlayer();
// player.reset();
// try {
// player.setDataSource("sdcard/2.3gp");
// //指定視頻播放在哪個surfaceview
// player.setDisplay(holder);
// player.prepare();
// player.start();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// });
// }
// };
// t.start();
holder.addCallback(new Callback() {
//surfaceview銷毀時會調用
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(player != null){
//停止之前先保存播放進度
progress = player.getCurrentPosition();
player.stop();
player.release();
player = null;
}
}
//surfaceview創建時會調用
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(player == null){
player = new MediaPlayer();
player.reset();
try {
player.setDataSource("sdcard/2.3gp");
//指定視頻播放在哪個surfaceview
player.setDisplay(holder);
player.prepare();
//跳轉到上一次停止的地方繼續播放
player.seekTo(progress);
player.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//surfaceview結構改變時會調用
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vv = (VideoView) findViewById(R.id.vv);
vv.setVideoPath("sdcard/2.3gp");
vv.start();
}
public void click1(View v){
//啟動拍照的Activity
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("sdcard/haha.jpg")));
startActivityForResult(intent, 10);
}
public void click2(View v){
//啟動拍照的Activity
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("sdcard/haha2.3gp")));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 20);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 10){
Toast.makeText(this, "拍照完畢", 0).show();
}
else if(requestCode == 20){
Toast.makeText(this, "攝像完畢", 0).show();
}
}
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkCameraHardware(this)){
// 創建攝像頭實例
mCamera = getCameraInstance();
}
else{
return;
}
//創建預覽類的對象
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
//把預覽類設置為幀布局的子節點
preview.addView(mPreview);
// 給按鈕設置點擊偵聽
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//自動聚焦
mCamera.autoFocus(new AutoFocusCallback() {
//聚焦完成調用
@Override
public void onAutoFocus(boolean success, Camera camera) {
// 拍照
mCamera.takePicture(null, null, mPicture);
}
});
}
}
);
}
/** 檢測手機是否有攝像頭 */
private boolean checkCameraHardware(Context context) {
//檢測手機是否安裝有攝像頭應用
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** 一個獲取攝像頭實例的安全途徑 */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // 獲取第一個後置攝像頭的實例
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private PictureCallback mPicture = new PictureCallback() {
//拍照會調用此方法
//data:照片的字節數組
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
File pictureFile = new File("sdcard/mrplus.jpg");
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(CameraPreview.TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(CameraPreview.TAG, "Error accessing file: " + e.getMessage());
} finally{
//拍照完成後重新進入預覽
camera.startPreview();
}
}
};
}
預覽類:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
public static final String TAG = "camera";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
//獲取surfaceview的控制器
mHolder = getHolder();
//設置偵聽
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
//設置攝像頭的預覽界面顯示在holder對應的那個surfaceview
mCamera.setPreviewDisplay(holder);
//開始預覽
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// 在對surfaceview做改變前,先停止預覽
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// 設置改變完後,重新開始預覽
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
前面分析過 u-boot1.1.6 三個階段的啟動過程,也嘗試自己寫了一個簡單的 bootloader ,是時候嘗試移植一個相對較新一點的 u-boot 了,看過韋東山老
概述在上文,酷炫Path動畫已經預告了,今天給大家帶來的是利用 純自定義View,實現的仿餓了麼加入購物車控件,自帶閃轉騰挪動畫的按鈕。 效果圖如下:圖1 項目中使用的效
研究增量更新的熱情被激發了,通過幾天的資料查找和學習,搞懂增量更新之余,也順便練習了下NDK開發。效果圖預覽開發環境Android Studio 2.2.1 For W
水波紋效果已經不是什麼稀罕的東西了,用過5.0新控件的小伙伴都知道這個效果,可是如果使用一個TextView或者Button或者其它普通控件的話,你是否知道如何給它設置水