I said a little detail: When the main interface, I set up a replacement of the main interface buttons, indeed by setBackgroundResource (R.drawable.bgimg2); can successfully set the background, but now I have one demand: open the phone sd card Pictures displayed in another activity, the user after long press pictures, pop-up dialog box: Set the main interface background, but by setBackgroundResource (R.drawable.bgimg2); This code can not modify the background of the main interface, ask how to solve ?
Reply:
R.drawable.bgimg2 applications engineering drawable resource, which is a resource mapping android, you have to reality sd card in the picture, the picture can only speak for the drawabel then loaded to achieve
by setBackgroundDrawable/ **
* Generated picture
*
* @ Param path
* @ Return
* /
public static Bitmap creatBitmap (String path) {
if (path == null | | path.equals ("")) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options ();
options.inPurgeable = true;
try {
Bitmap bitmap = BitmapFactory.decodeFile (path);
RocLog.info ("ViewUtil", "@ @ creatBitmap @ @ the is bitmap:" + bitmap);
return bitmap;
} Catch (Exception e) {
/ / TODO auto-generated catch block
e.printStackTrace ();
}
return null;
}
setBackgroundDrawable (new BitmapDrawable (creatBitmap (path)));
Reply:
Thank you for your answer, I tried this method, so there is no effect, but thanks for your advice
Reply:
Communication between the two Activity Through Intent, you can set startActivityForResult, to automatically set by the return code
Or use the Service and the broadcast can be achieved
Reply:
bttton.setBackgroundDrawable (new BitmapDrawable (BitmapFactory.decodeFile (SD card path)));
Reply:
First, this need not broadcast it. Pass an object can be resolved. .
Wrote a simple example. .
Click btn1 change the background to achieve the effect of
package com.lxl.testapp;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
Context context;
private NotificationManager mNotificationManager; / / notification management
int NOTIFICATIONS_ID = 111;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
this.context = this;
Log.e ("TEST", "MainActivity start");
setContentView (R.layout.activity_main);
/ / ImageView i = new ImageView (this);
/ / I.setBackgroundResource (R.drawable.showicon);
/ / WindowManager wm = (WindowManager) context.getSystemService (context.WINDOW_SERVICE);
/ / Wm.addView (i, new WindowManager.LayoutParams (-1, -1));
Listpacks = context.getPackageManager ()
. GetInstalledPackages (0);
mNotificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); / / Initialization Manager
final RelativeLayout r = (RelativeLayout) findViewById (R.id.layout);
final WaitDialog waitDialog = new WaitDialog (context);
Button btn1 = (Button) findViewById (R.id.btn1);
btn1.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
InputStream open = null;
try {
open = context.getResources () getAssets () open ("showicon.png");..
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
Drawable d = Drawable.createFromStream (open, "showicon.png");
r.setBackground (d);
}
});
Button btn2 = (Button) findViewById (R.id.btn2);
btn2.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
Intent i = new Intent ();
i.setClass (context, MyActivity.class);
context.startActivity (i);
}
});
Button btn3 = (Button) findViewById (R.id.btn3);
btn3.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
setWeather ("information", "title", "content", R.drawable.showicon);
}
});
}
@ Override
protected void onDestroy () {
Log.e ("TEST", "MainActivity end");
super.onDestroy ();
}
private void setWeather (String tickerText, String title, String content,
int drawable) {
/ / Create an instance of the notification, the first parameter is the picture, the title bar displays the text a second, and the third is the time
Notification noti = new Notification (drawable, tickerText,
System.currentTimeMillis ());
/ / When you click the drop-down down title when do here is to jump to the main interface. Here and below are together.
PendingIntent contentIntent = PendingIntent.getActivity (this, 0,
new Intent (), 0);
/ / Set the style, the default is the sound
noti.defaults = Notification.DEFAULT_SOUND;
/ / Title is to pull down the title, content Content is also a drop-down display after
noti.setLatestEventInfo (this, title, content, contentIntent);
/ / Show this notice
mNotificationManager.notify (NOTIFICATIONS_ID, noti);
}
}
No comments:
Post a Comment