我对Android完全陌生,但我使用了大量的StackOverflow问题来几乎完成我的应用程序。
简而言之,工作流程是:从图库上传图像,使用特定路径将其保存到内部存储中(这意味着,下一个上传的图像也将保存到完全相同的内部路径位置),然后将其设置为背景(位于现有背景之上,来自XML:android:background="@drawable/bg_app_img
,可以很好地工作)。
问题是,在保存级别和保存图像的墙纸设置方面,尝试这样做效果很好,但应用程序的背景效果不好。使用全窗口比例选项设置图像背景(如果它的ImageView
是XML文件中倒数第一个-最高优先级),它不能被当前上传的图像替换。如果XML中的ImageView order is opposite, from highest scaled to lowest, it would be an image above a larger prior chosen image (see image below). So the problem lies with the order of ImageView
:第三个(从顶部开始)是全尺寸的ImageView order is opposite, from highest scaled to lowest, it would be an image above a larger prior chosen image (see image below). So the problem lies with the order of ImageView
,它被“卡住”,不允许显示其他图像,而第二个不允许显示第一个图像(因为它会显示在它后面+更小)。XML可能定义了优先级,但在我的例子中是不必要的,还有after setting the background, even deleting the Image from internal storage does nothing to the currently shown last uploaded image pixels with their high priority
。
已尝试:
1.删除android:background="@drawable/bg_app_img
(a可能是(?)拉伸墙纸的解决方案,没有帮助)。
1.在设置新图像之前,再次设置bg_app_img
(编码方式相同):
Uri uri_bg_temp = Uri.parse("android.resource://" + getPackageName() + "/" +
R.drawable.bg_app_img);
full_ImageView.setImageURI(uri_bg_temp);
smaller_ImageView.setImageURI(yourUri);
不起作用,因为它使“bg_app_img”成为背景,而不是上面的新图像。
1.更改XML文件中3 ImageView
的顺序所做的更改没有解决问题,因为较小的ImageView
现在可以高于较大的大小(根据XML文件中的优先级):
1.删除android:scaleType="fitXY"
。不用了。
1.同步清除、重建所有缓存和使缓存无效,也没有帮助。
我不想永久更改XML(或应用程序当前打开的时间?)使用代码,因为用户可能选择半比例图像,然后是全比例图像,然后是半比例图像,如果以任何方式删除半(高度)比例的图像,则不会显示-这是不好的。
应该发生什么:从图库中选择的任何新图像都将出现在drawable/bg_app_img
上方的背景中,而不是以前从图库中选择的任何图像。
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_app_img"
tools:contetx=".day1">
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:text="Wallpaper preview, image size:"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="25sp"
android:translationZ="90dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image_view_smallest"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:adjustViewBounds="true"
android:scaleType="center"
app:layout_constraintWidth_percent="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintHeight_default="percent" />
<ImageView
android:id="@+id/image_view_smaller"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scaleType="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:adjustViewBounds="true"
app:layout_constraintWidth_percent="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintHeight_default="percent" />
<ImageView
android:id="@+id/image_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:adjustViewBounds="true"
app:layout_constraintWidth_percent="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_percent="1"
android:scaleType="fitXY"
app:layout_constraintHeight_default="percent" />
<Button
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:backgroundTint="@color/white"
android:text="Full"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="@+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chose_smaller_img" />
<Button
android:id="@+id/chose_smaller_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:backgroundTint="@color/white"
android:text="3/4"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="@+id/gallery"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chose_smallest_img" />
<Button
android:id="@+id/chose_smallest_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:backgroundTint="@color/white"
android:text="half"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="@+id/chose_smaller_img"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java类代码:
package com.my_app;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
public class day1 extends AppCompatActivity {
//creating a mutable (changeable) bitmap:
int w = 1, h = 1;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap btm = Bitmap.createBitmap(w, h, conf);
//Saving to internal memory the image as it is
private void saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/your_app/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File internal_image_saved_path=new File(directory,"day1.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(internal_image_saved_path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
}
//init:
Uri uri=null;
Context context=this;
//day in week:
Calendar c = Calendar.getInstance();
int day_in_week = c.get(Calendar.DAY_OF_WEEK);
//to scale image to window size (stretch)
Button full_PickImage;
ImageView full_ImageView;
ActivityResultLauncher<String> mGetContent;
//to scale image to window width and 0.75 height size
Button smaller_PickImage;
ImageView smaller_ImageView;
ActivityResultLauncher<String> smallerGetContent;
//to scale image to window width and 0.5 height size
Button smallest_PickImage;
ImageView smallest_ImageView;
ActivityResultLauncher<String> smallestGetContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day1);
//full size, 3/4, half:
full_PickImage = findViewById(R.id.gallery);
full_ImageView = findViewById(R.id.image_view);
smaller_PickImage = findViewById(R.id.chose_smaller_img);
smaller_ImageView = findViewById(R.id.image_view_smaller);
smallest_PickImage = findViewById(R.id.chose_smallest_img);
smallest_ImageView = findViewById(R.id.image_view_smallest);
mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
result -> {
full_ImageView.setImageURI(result);
uri = result;
if(uri!=null) {
//uri to bitmap to store in internal storage:
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver() , Uri.parse(uri.toString()));
//get window width & height size:
int[] win_size = win_size();
int win_width = win_size[0];
int win_height = win_size[1];
//bitmap scaled to win height & width size
Bitmap resized_bitmap = getResizedBitmap(bitmap, win_width, win_height);
//internal save:
saveToInternalStorage(resized_bitmap);
//set in App background:
show_background_chosen_img(0);
btm = resized_bitmap; //used later to set wallpaper (deleted for stackoverflow)
}
catch (Exception e) {
//handle exception
}
}
});
smallerGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
result -> {
smaller_ImageView.setImageURI(result);
uri = result;
if(uri!=null) {
//uri to bitmap to store in internal storage:
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver() , Uri.parse(uri.toString()));
//get win h&w size:
int[] win_size = win_size();
int win_width= win_size[0];
int win_height=(int)(win_size[1] * 0.75);
//bitmap scaled to win height & width size
Bitmap resized_bitmap = getResizedBitmap(bitmap, win_width, win_height);
saveToInternalStorage(resized_bitmap);
//set background:
show_background_chosen_img(1);
btm = resized_bitmap; //used later
}
catch (Exception e) {
//handle exception
}
}
});
smallestGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
result -> {
smallest_ImageView.setImageURI(result);
uri = result;
if(uri!=null) {
//uri to bitmap to store in internal storage:
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver() , Uri.parse(uri.toString()));
//get win h&w size:
int[] win_size = win_size();
int win_width= (int)(win_size[0]);
int win_height=(int)(win_size[1] * 0.5);
//bitmap scaled to window height & width size (from above...)
Bitmap resized_bitmap = getResizedBitmap(bitmap, win_width, win_height);
saveToInternalStorage(resized_bitmap);
//set background:
show_background_chosen_img(2);
btm = resized_bitmap; //used later
}
catch (Exception e) {
//handle exception
}
}
});
full_PickImage.setOnClickListener(v -> mGetContent.launch("image/*"));
smaller_PickImage.setOnClickListener(v -> smallerGetContent.launch("image/*"));
smallest_PickImage.setOnClickListener(v -> smallestGetContent.launch("image/*"));
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
// get phone window size
public int[] win_size(){
Display display = getWindowManager(). getDefaultDisplay();
Point size = new Point();
display. getSize(size);
int win_width = size.x;
int win_height = size.y;
return new int[] {win_width, win_height};
}
public void show_background_chosen_img(int type){
//show current loaded image at first enter to class
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/your_app/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File f = new File(directory, "day1.jpg");
if (f.exists()) {
Uri yourUri = Uri.fromFile(f);
if (type==0) {
//Uri uri_bg_temp = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.bg_app_img);
//full_ImageView.setImageURI(uri_bg_temp);
full_ImageView.setImageURI(yourUri);
}
else if (type==1){
//Uri uri_bg_temp = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.bg_app_img);
//full_ImageView.setImageURI(uri_bg_temp);
smaller_ImageView.setImageURI(yourUri); }
else if (type==2) {
//Uri uri_bg_temp = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.bg_app_img);
//smallest_ImageView.setImageURI(uri_bg_temp);
smallest_ImageView.setImageURI(yourUri); }
}
}
}
如果您觉得Main类及其XML,甚至build.gradle
也需要,请让我知道。B.t.w,bg_app_image.png
是e1d11d1文件(即使它可能与问题无关)。谢谢!
1条答案
按热度按时间pkwftd7m1#
解决这个问题的诀窍是,我将XML文件中的优先级设置为最小的图像,然后在尝试将完整图像应用于屏幕背景时,我将ImageView从完整更改为最小(具有最高优先级),这样:首先,我再次应用背景,以使用其他选项(Half,3/4...)重置已应用的旧背景:
然后,在背景图像的设置部分从完整(ImageView)变为最小(最高优先级):
设置应用程序背景: