我正在根据我正在处理的屏幕大小生成图像视图。我试图为缩略图放置一个占位符。在用户拍照后,将其放置在该占位符内并缩放以适合。我不确定这是否是缩放缩略图图像的正确方法。有没有人做过类似的事情?
CameraActivity:
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CameraActivity extends AppCompatActivity {
private static final String TAG = "CameraActivty";
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView mImageThumb;
private Button cameraButton;
private EditText note;
private RelativeLayout layout;
private String mCurrentPhotoPath;
private File photoFile;
private int targetW;
private int targetH;
InputMethodManager in;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#000000"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
layout = (RelativeLayout) findViewById(R.id.cameraRelLayout);
mImageThumb = (ImageView) findViewById(R.id.imageThumb);
getScreenSize();
mImageThumb.getLayoutParams().height = targetH;
mImageThumb.getLayoutParams().width = targetW;
cameraButton = (Button) findViewById(R.id.pictureButton);
note = (EditText) findViewById(R.id.note);
note.setScroller(new Scroller(this));
note.setMaxLines(1);
note.setVerticalScrollBarEnabled(true);
note.setMovementMethod(new ScrollingMovementMethod());
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
});
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
hideKeyboard(view);
return false;
}
});
note.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
showKeyboard(view);
return false;
}
});
}
private void getScreenSize(){
DisplayMetrics display = this.getResources().getDisplayMetrics();
int screenWidth = display.widthPixels;
targetW = screenWidth - (screenWidth / 8);
targetH = (targetW * 3) / 4;
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
setPic();
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void setPic() {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
mImageThumb.setImageBitmap(rotateBitmap(bmOptions));
}
public Bitmap rotateBitmap(BitmapFactory.Options ops){
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, ops);
Bitmap scaledMap = Bitmap.createScaledBitmap(bitmap, targetW, targetH, true);
Bitmap rotatedMap = Bitmap.createBitmap(scaledMap, 0, 0, scaledMap.getWidth(), scaledMap.getHeight(), matrix, true);
return rotatedMap;
}
public void hideKeyboard(View view) {
in.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
public void showKeyboard(View view) {
in.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
@Override
protected void onResume() {
super.onResume();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
CameraXML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:id="@+id/cameraRelLayout">
<ImageView
android:layout_marginTop="25dp"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="@drawable/image_holder"
android:layout_centerHorizontal="true"
android:id="@+id/imageThumb"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp"
android:layout_alignParentBottom="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:enabled="true"
android:minLines="2"
android:textIsSelectable="true"
android:focusable="true"
android:isScrollContainer="true"
android:layout_weight="1"
android:gravity="top"
android:padding="10dp"
android:ems="10"
android:hint="Problem Description"
android:layout_marginBottom="15dp"
android:id="@+id/note"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/pictureButton"
android:background="@drawable/default_button"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="8dp" />
</LinearLayout>
</RelativeLayout>