即时通讯寻找在同一个布局上有两个按钮,您单击第一个按钮,选择一个图像,该按钮将变为所选图像。您单击第二个按钮并选择该图像将替换该按钮。最容易使用imageButton而不是ImageView。 如果可能,我想要代码,谢谢。从图库加载图像到ImageButton(查看)
(静止不明白的端应为2倍的图像彼此相邻由用户选择?)
MainActivity:
package com.example.triptych;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageButton imageButton = (ImageButton) findViewById(R.id.buttonLoadPicture);
// Set the Image in ImageView after decoding the String
imageButton.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="@drawable/ic_launcher"
android:text="@string/load_picture" />
<ImageButton
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="@drawable/ic_launcher"
android:text="@string/load_picture" />
</RelativeLayout>
另一个问题,我是否为第二个按钮创建另一个活动并粘贴相同的代码,或者我是否对同一活动执行此操作?
这里的每个人都不会为你写代码。如果你有书面的代码,然后告诉我们。我们可以帮助解决问题。 @Harrison – Pooja
我已经编写了代码,我将编辑帖子,但是我注意到每次写出代码时,我都会在帖子中收到负面反馈。 – Harrison
负面反馈背后可能有某种原因。 @Harrison – Pooja