刷新活动/ ImageView?

时间:2012-12-14 00:40:07

标签: android

在我的第一个活动中,我有一个按钮和一个webview,当我点击按钮时,它会在一个imageview中打开一个包含webview内容的新活动。 (我在位图中转换webview并使用此位图设置imageview)。

问题是当我第一次点击按钮时,没有任何反应,我的屏幕保持白色。但是,如果我回到第一个活动并重新点击按钮,则会很好地显示imageview。

为什么第一次显示的是imageview?我是否需要刷新我的第二个活动或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;
    }

}

我不知道它是否已链接但是当我启动我的应用程序时,我在我的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.

1 个答案:

答案 0 :(得分:0)

我强烈推荐一种不同的方法 通过意图传递整个位图需要大量内存,并且不能在旧手机上工作,比如说SDK 2.3 尝试将位图作为byteArray传递并构建它以便在下一个活动中显示。 如果你想在活动之间传递它,我会将它存储在一个文件中。这样效率更高,而且工作量更少。您可以使用任何其他应用无法访问的MODE_PRIVATE在数据文件夹中创建私人文件 顺便说一句,以下内容对我有用:

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);
}

接收:

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");
        }

    }


}