【发布时间】:2017-06-25 12:03:47
【问题描述】:
请帮助我,到目前为止,我已经编写了一个代码,可以从相机和图库中选择图像并显示它,但我发现很难裁剪它。我正在尝试使用一个名为 android imagecropper 的第三方 android 库,但发现它很难实现。我已经玩过代码,但没有任何有用的东西出现。换句话说,我正在尝试实现在 whasap 中获得的效果,您可以在其中单击按钮上传个人资料图片,将其设置为裁剪,然后显示图像的圆形视图。这是 android 库 android image cropper 的链接。任何帮助将不胜感激谢谢大家。
这是我的java代码
public class MainActivity extends Activity {
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private ImageView ivImage;
private String userChoosenTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
btnSelect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
ivImage = (ImageView) findViewById(R.id.ivImage);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
//code for deny
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(MainActivity.this);
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
// Ezphoto picker in action
private void galleryIntent()
{
EZPhotoPickConfig config = new EZPhotoPickConfig();
config.photoSource = PhotoSource.GALERY;
config.needToExportThumbnail = true;
config.exportingThumbSize = 200;
config.exportingSize = 1000;
EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
}
private void cameraIntent()
{
EZPhotoPickConfig config = new EZPhotoPickConfig();
config.photoSource = PhotoSource.CAMERA;
config.exportingSize = 1000;
EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK){
return;
}
if(requestCode == EZPhotoPick.PHOTO_PICK_REQUEST_CODE){
try {
Bitmap pickedPhoto = new EZPhotoPickStorage(this).loadLatestStoredPhotoBitmapThumbnail();
ivImage.setImageBitmap(pickedPhoto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的 xml 代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" >
<Button
android:id="@+id/btnSelectPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Photo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
标签: java android xml imageview