如何在android中添加两个图像视图和两个按钮
在我的项目中,我将使用单一布局在图像上进行签名。我将把布局分成两个线性布局。在第一个线性布局中,我将从相机中获取图像,因为我使用相机按钮和图像视图,在第二个线性布局中,我将从画廊中获取图像,因为我使用一个图像视图和画廊按钮。如果我点击相机或画廊按钮,图像将出现在图像视图上,但在我的项目中,我无法得到这样的效果,我在布局上困惑了如何给出这种布局 这是xml文件代码上部布局用于画廊部分,下部布局用于相机部分如何在android中添加两个图像视图和两个按钮,android,image,android-layout,android-imageview,android-button,Android,Image,Android Layout,Android Imageview,Android Button,在我的项目中,我将使用单一布局在图像上进行签名。我将把布局分成两个线性布局。在第一个线性布局中,我将从相机中获取图像,因为我使用相机按钮和图像视图,在第二个线性布局中,我将从画廊中获取图像,因为我使用一个图像视图和画廊按钮。如果我点击相机或画廊按钮,图像将出现在图像视图上,但在我的项目中,我无法得到这样的效果,我在布局上困惑了如何给出这种布局 这是xml文件代码上部布局用于画廊部分,下部布局用于相机部分 <?xml version="1.0" encoding="utf-8"?> &
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivgallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bgallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivcamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bcamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
画廊
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
//试试这个方法,希望这能帮助你 活动\u main.xml AndroidManifest.xml请将“com.example.Demo”替换为“包名”
您能发布您尝试过的内容吗?在take 2 linearlayout中添加相关布局…在布局中保留图像视图和按钮(一个用于相机,另一个用于画廊)我已经使用了这段代码,但它不能正常工作。我已经实现了你的评论,我得到了一些好的建议。我使用了这段代码一些不同的想法,我得到了一个很好的例子。
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="@+id/imgFromCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Camera"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="@+id/imgFromGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<Button
android:id="@+id/btnGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gallery"/>
</LinearLayout>
private static final int GET_GALLERY_IMAGE = 1;
private static final int GET_CAMERA_IMAGE = 2;
Button btnCamera;
Button btnGallery;
ImageView imgFromGallery;
ImageView imgFromCamera;
private String imgPath;
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
btnGallery = (Button) findViewById(R.id.btnGallery);
btnCamera = (Button) findViewById(R.id.btnCamera);
imgFromCamera = (ImageView) findViewById(R.id.imgFromCamera);
imgFromGallery = (ImageView) findViewById(R.id.imgFromGallery);
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), GET_GALLERY_IMAGE);
}
});
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, GET_CAMERA_IMAGE);
}
});
}
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == GET_GALLERY_IMAGE) {
imgFromGallery.setImageBitmap(decodeFile(getAbsolutePath(data.getData())));
} else if (requestCode == GET_CAMERA_IMAGE) {
imgFromCamera.setImageBitmap(decodeFile(getImagePath()));
}
}
}
public String getAbsolutePath(Uri uri) {
if(Build.VERSION.SDK_INT >= 19){
String id = uri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
final String imageOrderBy = null;
Uri tempUri = getUri();
Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}else{
return null;
}
}else{
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}
private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>