允许用户从个性化屏幕背景 [英] Allow user to select background from personalize screen

查看:129
本文介绍了允许用户从个性化屏幕背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的小部件,我希望用户能够通过选择他们自己的相册图片设置主页的背景,然后是形象也将在个性化屏幕上ImageView的显示出来(让他们知道什么图像的样子)。
到目前为止,我已经在ImageView的设置个性化页面上:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
    机器人:ID =@ + ID / personalizetextView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/自定义
    机器人:TEXTSIZE =30dip
    机器人:比重=中心
    机器人:layout_marginTop =20dip/><的TextView
    机器人:ID =@ + ID / personalizetextviewChangeBackground
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/ customizebackground
    机器人:比重=中心/>< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@绘制/样式1/><按钮
    机器人:ID =@ + ID / btnChangeImage
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/ change_background/>< / LinearLayout中>

在选择btnChangeImage,它会导致用户在画廊,因为在这里看到:

 包com.example.awesomefilebuilderwidget;进口java.io.BufferedInputStream中;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;公共类个性化延伸活动{
Button按钮;
ImageView的形象;
私有静态最终诠释SELECT_PICTURE = 1;
私人字符串selectedImagePath;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.personalize);    addListenerOnButton();}公共无效addListenerOnButton(){    图像=(ImageView的)findViewById(R.id.imageView1);    按钮=(按钮)findViewById(R.id.btnChangeImage);
    button.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            意向意图=新的Intent();
            intent.setType(图像/ *);
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(意向,SELECT_PICTURE);
        }        公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
            {
                如果(结果code == RESULT_OK){
                    如果(要求code == SELECT_PICTURE)
                    {
                        乌里selectedImageUri = data.getData();
                        selectedImagePath =的getPath(selectedImageUri);
                        尝试{
                            的FileInputStream fileis =新的FileInputStream(selectedImagePath);
                            的BufferedInputStream bufferedstream =新的BufferedInputStream(fileis);
                            字节[] = bMapArray新的字节[bufferedstream.available()];
                            bufferedstream.read(bMapArray);
                            位图BMAP = BitmapFactory.de codeByteArray的(bMapArray,0,bMapArray.length);
                            //在这里,您可以将此/位图图像设置为按钮背景图片                            如果(fileis!= NULL)
                            {
                                fileis.close();
                            }
                            如果(bufferedstream!= NULL)
                            {
                                bufferedstream.close();
                            }
                        }赶上(FileNotFoundException异常五){
                            e.printStackTrace();
                        }赶上(IOException异常五){
                            e.printStackTrace();
                        }
                    }
                }
            }
        公共字符串的getPath(URI URI){
                的String [] =投影{MediaStore.Images.Media.DATA};
                光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
                INT与Column_Index =光标
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                返回cursor.getString(Column_Index中);
            }    });}}

我怎样才能让这个被选中的图像时,该图像显示在该ImageView的?然后,无论图像被设置在ImageView的成为主文件的背景是什么?

目前在主页上,我只是有一个默认的图像设置:

 的android:背景=@绘制/样式1


解决方案

这ActivityResult设置你的图像ImageView的:

  image.setImageBitmap(BMAP);

图像保存,所以你可以再次或您的主要活动使用它:

 公共布尔saveImageToInternalStorage(位图图像​​){
   尝试{
      FOS的FileOutputStream = context.openFileOutput(desiredFilename.png,Context.MODE_PRIVATE);
      image.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
      fos.close();
      返回true;
   }赶上(例外五){
   返回false;
   }
}

加载图像:

 公共位图getThumbnail(字符串文件名){
   位图的缩略图= NULL;
   尝试{
      文件文件路径= context.getFileStreamPath(文件名);
      科幻的FileInputStream =新的FileInputStream(文件路径);
      缩略图= BitmapFactory.de codeStream(FI);
   }赶上(例外前){
   Log.e(getThumbnail()的内部存储空间,ex.getMessage());
   }
   返回缩略图;
}

设置加载的图像为背景在您的主页:

 可绘制D =新BitmapDrawable(getResources(),位图);
yourBackgroundView.setBackground(四);

In my widget, I want the user to be able to set the background of the main page by selecting a picture from their gallery and then that image would also show up in a imageView on the personalize screen (so they know what the image will look like). So far, I have the imageView set up on the personalize page:

<?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" >

<TextView
    android:id="@+id/personalizetextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customize" 
    android:textSize="30dip"
    android:gravity="center"
    android:layout_marginTop="20dip"/>

<TextView 
    android:id="@+id/personalizetextviewChangeBackground"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customizebackground"
    android:gravity="center" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pattern1" />

<Button
    android:id="@+id/btnChangeImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_background" />

</LinearLayout>

When the btnChangeImage is selected, it leads the user to the gallery, as seen here:

package com.example.awesomefilebuilderwidget;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Personalize extends Activity{
Button button;
ImageView image;
private static final int SELECT_PICTURE = 1;
private String  selectedImagePath;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personalize);

    addListenerOnButton();

}

public void addListenerOnButton() {

    image = (ImageView) findViewById(R.id.imageView1);

    button = (Button) findViewById(R.id.btnChangeImage);
    button.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PICTURE);
        }

        public void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                if (resultCode == RESULT_OK) {
                    if (requestCode == SELECT_PICTURE)
                    {
                        Uri selectedImageUri = data.getData();
                        selectedImagePath = getPath(selectedImageUri);
                        try {
                            FileInputStream fileis=new FileInputStream(selectedImagePath);
                            BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
                            byte[] bMapArray= new byte[bufferedstream.available()];
                            bufferedstream.read(bMapArray);
                            Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
                            //Here you can set this /Bitmap image to the button background image

                            if (fileis != null) 
                            {
                                fileis.close();
                            }
                            if (bufferedstream != null) 
                            {
                                bufferedstream.close();
                            }
                        } catch (FileNotFoundException e) {                 
                            e.printStackTrace();
                        } catch (IOException e) {                   
                            e.printStackTrace();
                        }               
                    }
                }
            }


        public String getPath(Uri uri) {
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }

    });

}

}

How can I make it so that when the image is selected, that image shows up in the imageview? Then, whatever image is set up in the imageview becomes the background of the main file?

Right now on the main page, I just have a default image set up:

android:background="@drawable/pattern1" 

解决方案

Set your image from ActivityResult to ImageView:

image.setImageBitmap(bMap);

Save this image so you can use it again or in your main activity:

public boolean saveImageToInternalStorage(Bitmap image) {
   try {
      FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
      image.compress(Bitmap.CompressFormat.PNG, 100, fos);
      fos.close();   
      return true;
   } catch (Exception e) {
   return false;
   }
}

Load your image:

public Bitmap getThumbnail(String filename) {
   Bitmap thumbnail = null;
   try {
      File filePath = context.getFileStreamPath(filename);
      FileInputStream fi = new FileInputStream(filePath);
      thumbnail = BitmapFactory.decodeStream(fi);
   } catch (Exception ex) {
   Log.e("getThumbnail() on internal storage", ex.getMessage());
   }
   return thumbnail;
}

Set the loaded image as Background in your main page:

Drawable d = new BitmapDrawable(getResources(),bitmap);
yourBackgroundView.setBackground(d);

这篇关于允许用户从个性化屏幕背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆