刷新活动/ ImageView的? [英] Refresh activity/ImageView?
问题描述
在我的第一个活动我有一个按钮和一个web视图,当我点击按钮,它会打开web视图的在ImageView的内容的新的活动。 (我改造的WebView位图,并设定该位图的ImageView)。
问题是,当我点击按钮的第一次,没有发生,我的屏幕保持白色。但如果我回来的第一个活动,并reclick按钮上的ImageView的很好显示。
为什么在第一次没有在ImageView的显示?我是否需要像刷新我的第二个活动或ImageView的?
WebViewActivity.java
公共类WebViewActivity延伸活动{ 私人的WebView web视图;
ImageView的ImageView的;
Button按钮;
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.webview); web视图=(的WebView)findViewById(R.id.webView1);
webView.getSettings()setJavaScriptEnabled(真)。
字符串customHtml =&LT; BODY BACKGROUND = \\pic_10.jpg \\&GT;&LT; BODY&GT;&LT; DIV的风格= \\的立场:绝对的;底部:0;宽度:100%\\&GT;&LT; DIV的风格= \\填充:24px的0 0;保证金:0;高度:40像素;背景:透明;位置:亲属; \\&GT;&LT; A HREF = \\HTTP: //www.amazon.com \\的风格= \\填充:0 140px 0 0;保证金:0;高度:40像素;显示:块;文字修饰:无;溢出:隐藏;背景:#FFF; \\的目标= \\_空白\\&GT;&LT;跨度风格= \\填充:0 0 0 40像素;保证金:0;显示:块;溢出:隐藏;背景:透明的网址(http://ads.gumgum.com/com /gumgum/tests/amazon.png)不重复向左滚动中心; \\&GT;&LT; p风格= \\填充:0;保证金:0;字体:12px的黑体/ 16px的Arial;color:#000;text-decoration:underline;display:block;height:16px;overflow:hidden;text-align:left;padding:2px 0;自动换行:打破字; \\&GT; HBase的管理食谱&LT; / P&GT;&LT; p风格= \\填充:0;保证金:0;字体:12px的斜体/ 16px的Arial;color:#c60;text-decoration:none;display:block;height:16px;overflow:hidden;text-align:left;word-wrap:break-word;\\\">www.amazon.com</p></span><span风格= \\的立场:绝对的;底部:0;右:70像素;溢出:隐藏; \\&GT;&LT; IMG SRC = \\http://ecx.images-amazon.com/images/I/41OEZDHmUoL._SL75_ .JPG \\ style=\\\"border:none;max-width:64px;*width:64px;height:64px;\\\"></span></a></div></div>\";
webView.loadDataWithBaseURL(文件:/// android_asset /,customHtml,text / html的,UTF-8,NULL); 按钮=(按钮)findViewById(R.id.btnChangeImage); } 公共无效的sendMessage(查看视图){ webView.setWillNotCacheDrawing(假);
webView.destroyDrawingCache();
webView.setDrawingCacheEnabled(真);
webView.measure(MeasureSpec.makeMeasureSpec(480,MeasureSpec.EXACTLY)
MeasureSpec.makeMeasureSpec(800,MeasureSpec.EXACTLY));
webView.layout(0,0,webView.getMeasuredWidth(),webView.getMeasuredHeight());
webView.buildDrawingCache(真);
位图BMAP = Bitmap.createBitmap(webView.getDrawingCache());
webView.destroyDrawingCache();
//imageView.setMaxHeight(55);
//imageView.setMaxWidth(20);
位图bmap_New = scaleDownBitmap(BMAP,400,这一点);
意向意图=新意图(这一点,ImageViewActivity.class);
intent.putExtra(BitmapImage的bmap_New);
startActivity(意向);
} 公共静态位图scaleDownBitmap(位图照片,诠释newHeight,上下文的背景下){ 。最终浮动densityMultiplier = context.getResources()getDisplayMetrics()密度。 INT H =(INT)(newHeight * densityMultiplier);
INT W =(INT)(H * photo.getWidth()/((双)photo.getHeight())); 照片= Bitmap.createScaledBitmap(照片,W,H,真正的); 返回的照片;
}
ImageViewActivity.java
公共类ImageViewActivity延伸活动{ ImageView的ImageView的; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_image_view); ImageView的=(ImageView的)findViewById(R.id.imageView1);
意向意图= getIntent();
BMP位=(位图)intent.getParcelableExtra(BitmapImage的);
imageView.setImageBitmap(BMP);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.activity_image_view,菜单);
返回true;
}}
我不知道,如果是挂,但是当我开始我的应用我有这个在我的目录下载:
E /铬(32671):外部/铬/ NET / disk_cache / stat_hub.cc:190:[103321分之1214:ERROR:stat_hub.cc(190)] StatHub ::不支持应用程序com.example.androidtestadserver - 初始化。
我会强烈建议不同的方法。结果
经过意图整个位图需要大量的内存和不会工作较旧的手机上可以说SDK 2.3。结果
尝试传递位图作为的字节数组,并在接下来的活动构建它来显示。
如果你想通过它插图中的活动,我将其存储在一个文件中。这是更有效率,并为你工作越少。您可以创建在你的数据文件夹使用MODE_PRIVATE不属于任何其他应用程序访问私人文件。结果
通过下面的工作对我的方式:
公共无效的sendMessage(查看视图){ webView.setWillNotCacheDrawing(假);
webView.destroyDrawingCache();
webView.setDrawingCacheEnabled(真);
webView.measure(MeasureSpec.makeMeasureSpec(480,MeasureSpec.EXACTLY)
MeasureSpec.makeMeasureSpec(800,MeasureSpec.EXACTLY));
webView.layout(0,0,webView.getMeasuredWidth(),webView.getMeasuredHeight());
webView.buildDrawingCache(真);
位图BMAP = Bitmap.createBitmap(webView.getDrawingCache());
webView.destroyDrawingCache();
//imageView.setMaxHeight(55);
//imageView.setMaxWidth(20);
位图bmap_New = scaleDownBitmap(BMAP,400,这一点);
意向意图=新意图(这一点,ImageViewActivity.class);
//位图B: //位图
ByteArrayOutputStream BS =新ByteArrayOutputStream();
bmap_New.com preSS(Bitmap.Com pressFormat.PNG,50,BS);
intent.putExtra(BitmapImage的,bs.toByteArray());
//intent.putExtra(\"BitmapImage,bmap_New);
startActivity(意向);
}
接收:
公共类ImageViewActivity延伸活动{ ImageView的ImageView的; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_image_view); Log.d(ImageViewActivity,在OnCreate); ImageView的=(ImageView的)findViewById(R.id.imageView1);
//意向意图= getIntent();
// BMP位=(位图)intent.getParcelableExtra(BitmapImage的);
如果(getIntent()。getExtras()!= NULL){
位图B = BitmapFactory.de codeByteArray的(
。getIntent()getByteArrayExtra(BitmapImage的),0,getIntent()getByteArrayExtra(的BitmapImage)长度)。;
imageView.setImageBitmap(二); }
其他{
Log.d(ImageViewActivity,空);
} }
}
In my first activity I have a button and a webview and when I click on the button it opens a new activity with the content of the webview in an imageview. (I transform the webview in bitmap and set the imageview with this bitmap).
The problem is when I click on the button for the first time, nothing happen, my screen stays white. But if I come back in the first activity and reclick on the button the imageview is well displayed.
Why in the first time wasn't the imageview displayed? Do I need something like to refresh my second activity or Imageview?
WebViewActivity.java
public class WebViewActivity extends Activity {
private WebView webView;
ImageView imageView;
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<BODY BACKGROUND=\"pic_10.jpg\"><BODY><div style=\"position: absolute; bottom: 0; width: 100%\"><div style=\"padding:24px 0 0;margin:0;height:40px;background:transparent;position:relative;\"><a href=\"http://www.amazon.com\" style=\"padding:0 140px 0 0;margin:0;height:40px;display:block;text-decoration:none;overflow:hidden;background:#fff;\" target=\"_blank\"><span style=\"padding:0 0 0 40px;margin:0;display:block;overflow:hidden;background:transparent url(http://ads.gumgum.com/com/gumgum/tests/amazon.png) no-repeat scroll left center;\"><p style=\"padding:0;margin:0;font:bold 12px/16px Arial;color:#000;text-decoration:underline;display:block;height:16px;overflow:hidden;text-align:left;padding:2px 0;word-wrap:break-word;\">HBase Administration Cookbook</p><p style=\"padding:0;margin:0;font:italic 12px/16px Arial;color:#c60;text-decoration:none;display:block;height:16px;overflow:hidden;text-align:left;word-wrap:break-word;\">www.amazon.com</p></span><span style=\"position:absolute;bottom:0;right:70px;overflow:hidden;\"><img src=\"http://ecx.images-amazon.com/images/I/41OEZDHmUoL._SL75_.jpg\" style=\"border:none;max-width:64px;*width:64px;height:64px;\"></span></a></div></div>";
webView.loadDataWithBaseURL("file:///android_asset/", customHtml , "text/html", "utf-8", null);
button = (Button) findViewById(R.id.btnChangeImage);
}
public void sendMessage(View view) {
webView.setWillNotCacheDrawing(false);
webView.destroyDrawingCache();
webView.setDrawingCacheEnabled(true);
webView.measure(MeasureSpec.makeMeasureSpec(480, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY));
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
webView.buildDrawingCache(true);
Bitmap bmap = Bitmap.createBitmap(webView.getDrawingCache());
webView.destroyDrawingCache();
//imageView.setMaxHeight(55);
//imageView.setMaxWidth(20);
Bitmap bmap_New = scaleDownBitmap(bmap,400,this);
Intent intent = new Intent(this, ImageViewActivity.class);
intent.putExtra("BitmapImage", bmap_New);
startActivity(intent);
}
public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {
final float densityMultiplier = context.getResources().getDisplayMetrics().density;
int h= (int) (newHeight*densityMultiplier);
int w= (int) (h * photo.getWidth()/((double) photo.getHeight()));
photo=Bitmap.createScaledBitmap(photo, w, h, true);
return photo;
}
ImageViewActivity.java
public class ImageViewActivity extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
imageView = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent();
Bitmap bmp = (Bitmap)intent.getParcelableExtra("BitmapImage");
imageView.setImageBitmap(bmp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_image_view, menu);
return true;
}
}
I don't know if it is linked but when I start my application I have this in my catlog :
E/chromium(32671): external/chromium/net/disk_cache/stat_hub.cc:190: [1214/103321:ERROR:stat_hub.cc(190)] StatHub::Init - App com.example.androidtestadserver isn't supported.
I would highly recommend a different approach.
Passing the entire bitmap through intent requires a lot of memory and wont work on older phone lets say SDK 2.3.
Try passing the bitmap as a byteArray and build it for display in the next activity.
If you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.
By the way the following worked for me:
public void sendMessage(View view) {
webView.setWillNotCacheDrawing(false);
webView.destroyDrawingCache();
webView.setDrawingCacheEnabled(true);
webView.measure(MeasureSpec.makeMeasureSpec(480, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY));
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
webView.buildDrawingCache(true);
Bitmap bmap = Bitmap.createBitmap(webView.getDrawingCache());
webView.destroyDrawingCache();
//imageView.setMaxHeight(55);
//imageView.setMaxWidth(20);
Bitmap bmap_New = scaleDownBitmap(bmap,400,this);
Intent intent = new Intent(this, ImageViewActivity.class);
//Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bmap_New.compress(Bitmap.CompressFormat.PNG, 50, bs);
intent.putExtra("BitmapImage", bs.toByteArray());
//intent.putExtra("BitmapImage", bmap_New);
startActivity(intent);
}
Receiving:
public class ImageViewActivity extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Log.d("ImageViewActivity", "Oncreate");
imageView = (ImageView) findViewById(R.id.imageView1);
//Intent intent = getIntent();
//Bitmap bmp = (Bitmap)intent.getParcelableExtra("BitmapImage");
if( getIntent().getExtras() != null ){
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("BitmapImage"),0,getIntent().getByteArrayExtra("BitmapImage").length);
imageView.setImageBitmap(b);
}
else{
Log.d("ImageViewActivity", "null");
}
}
}
这篇关于刷新活动/ ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!