我的Android应用程序中有两个页面,我想将第一个页面的背景设置为第二个页面上Image View中显示的图像。我如何以编程方式实现这一点?我一直在尝试将Android应用程序中第二页的背景设置为第一页Image View中显示的图像。我已经尝试实现这一点,但它似乎并不像预期的那样工作。有人能检查我的代码并提供如何正确操作的指导吗?
第一活动,从该第一活动获取图像视图
using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.Graphics;
using SQLite;
namespace HAMANIA_APP
{
[Activity(Label = "GallerySaver")]
public class GallerySaver : Activity
{
Button camera, save, uploadPhoto;
ImageView pic;
Bitmap bitmap;
bool mExternalStorageAvailable = false;
bool mExternalStorageWriteable = false;
public static readonly int imageid = 1000;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Phone_Gallery_Saver);
camera = FindViewById<Button>(Resource.Id.btnCamera);
save = FindViewById<Button>(Resource.Id.btnSave);
pic = FindViewById<ImageView>(Resource.Id.gallery);
save.Click += Save_Click;
camera.Click += Camera_Click;
// Create your application here
}
private void Save_Click(object sender, EventArgs e)
{
Intent inte = new Intent();
inte.SetType("image/*");
inte.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(inte, "Select Picture"), imageid);
Toast.MakeText(Application.Context, "Image saved to gallery", ToastLength.Long).Show();
}
private void Camera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 0)//coming from camera
{
if (resultCode == Result.Ok)
{
Bundle extras = data.Extras;
bitmap = (Bitmap)extras.Get("data");
pic.SetImageBitmap(bitmap);
//bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");
// pic.SetImageBitmap(bitmap);
}
}else
{
if ((requestCode == imageid) && (resultCode == Result.Ok) && (data != null))
{
Android.Net.Uri uri = data.Data;
bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, uri);
pic.SetImageBitmap(bitmap);
var image = (Bitmap)data.Extras.Get("data");
}
}
}
}
}
第二页,用于设置背景
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Essentials;
using Android.Provider;
using Java.IO;
using System.IO;
using Android.Net;
using AndroidX.Core.Content;
using Android.Graphics.Drawables;
namespace HAMANIA_APP
{
[Activity(Label = "MenuActivity")]
public class MenuActivity : Activity
{
Button camera, gallery, control;
BroadcastBattery broadcastBattery;
TextView tv;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Menu);
camera = FindViewById<Button>(Resource.Id.BLUETOOTH);
tv = FindViewById<TextView>(Resource.Id.tv);
broadcastBattery = new BroadcastBattery(tv);
control = FindViewById<Button>(Resource.Id.control);
control.Click += Control_Click;
gallery = FindViewById<Button>(Resource.Id.gallery);
camera.Click += Camera_Click;
}
public static string BackgroundImagePath { get; set; }
private void Control_Click(object sender, EventArgs e)
{
Intent intent = new Intent(this, typeof(ControlCenter));
StartActivity(intent);
}
private void Camera_Click(object sender, EventArgs e)
{
Intent intent2 = new Intent(this, typeof(GallerySaver));
StartActivity(intent2);
}
}
}
我很感激你的帮助
1条答案
按热度按时间jm2pwxwz1#
您可以尝试将位图转换为字节数组,并通过
Intent
将其传递回MenuActivity
。根据你的代码,我创建了一个演示,并实现了这个功能。它在我这边工作正常。
可以参考以下代码:
MenuActivity.cs
为了从活动
GallerySaver
接收数据,我将代码StartActivity(intent2);
替换为StartActivityForResult
方法Camera_Click
:并覆盖方法
OnActivityResult
。请参考以下代码:
GallerySaver.cs
捕获图片后,您可以将位图转换为字节数组并通过Intent传递回来,并调用
SetResult
。GallerySaver.cs
的完整代码: