自定义对话框上的图像视图

时间:2012-11-21 11:50:56

标签: android android-layout android-asynctask imageview android-alertdialog

我在android中有自定义对话框的一个问题,我在android中使用javainterface创建了自定义对话框,在我的类CustomizeDialog中我有一个线程在后台做了一些处理之后我已经将一个imgae设置为这个自定义对话框我在OnPostExecute中设置图像,但我得到以下错误

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我知道我们无法从另一个线程访问主ui但我们在我的情况下访问OnPostExecuted中的主ui也无法正常工作, 在我的另一个类(main.class)中,我将通过

调用此类
CustomizeDialog customizeDialog = new CustomizeDialog(mContext);
customizeDialog.show();

这是我创建对话框的代码

public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
ImageView img = null;
ImageView img2=null;
ImageView img3 = null;
ImageView img4= null;
 Bitmap bm;

public CustomizeDialog(Context context) {
super(context);

img2 = (ImageView) findViewById(R.id.imageView1);
img3 = (ImageView) findViewById(R.id.imageView3);
img4 = (ImageView) findViewById(R.id.imageView4);

WindowManager.LayoutParams params = getWindow().getAttributes();  
params.x = 30;  
params.height = 500;  
params.width = 500;  
params.y = 50;  

this.getWindow().setAttributes(params);  

setContentView(R.layout.test);
img = (ImageView) findViewById(R.id.imageView2);

img.setBackgroundResource(R.drawable.sample);

AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 //Start the animation (looped playback by default).
 frameAnimation.start();

new LongOperation1(context).execute("");

}

else
{
    Toast toast = Toast.makeText(getContext(), "Erorr",
            Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.show();
} }

private class LongOperation1 extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
     //here i do some process
    return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {

                 // here i do some process to get bm

            img2.setImageBitmap(bm);
            }

    @Override
    protected void onPreExecute() {
    }
    @Override
    protected void onProgressUpdate(String... values) {
        }
}

AndroidHTMLActivity类

    package com.example.hellogap;
import com.example.javainterface.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidHTMLActivity extends Activity {

WebView myBrowser;
private static final int TEXT_ID = 0;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     myBrowser = (WebView)findViewById(R.id.mybrowser);
     myBrowser.addJavascriptInterface(new MyJavaScriptInterface(this), "AndroidFunction");

     myBrowser.getSettings().setJavaScriptEnabled(true);
     myBrowser.loadUrl("file:///android_asset/mypage.html");

 }

public class MyJavaScriptInterface {
 Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }

    public void showToast(String toast){
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    public void openAndroidDialog(){
        //CustomizeDialog.this.img2.setImageBitmap(bm);

        CustomizeDialog customizeDialog = new CustomizeDialog(AndroidHTMLActivity.this);
        customizeDialog.show();


    }

}
}

的test.xml     

     <AbsoluteLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/widget0"
         android:layout_width="500dp"
         android:layout_height="520dp"

         android:background="@drawable/bg"
         android:clickable="false" >

         <ImageView
             android:id="@+id/imageView3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_x="193dp"
             android:layout_y="315dp"
             android:src="@drawable/fb" />

         <ImageView
             android:id="@+id/imageView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_x="51dp"
             android:layout_y="318dp"
             android:src="@drawable/fa" />

         <ImageView
             android:id="@+id/imageView4"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_x="335dp"
             android:layout_y="316dp"
             android:src="@drawable/fc" />

         <ImageView
             android:id="@+id/imageView2"
             android:layout_width="200dp"
             android:layout_height="204dp"
             android:layout_x="298dp"
             android:layout_y="91dp" />

     </AbsoluteLayout>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"

   />
<WebView
   android:id="@+id/mybrowser"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
  />
</LinearLayout>

的mypage.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
 function showAndroidToast(toast) {
     AndroidFunction.showToast(toast);
 }
</script>

<input type="button" value="Open Dialog" onClick="openAndroidDialog()" />

<script type="text/javascript">
 function openAndroidDialog() {
     AndroidFunction.openAndroidDialog();
 }
</script>

</body>
</html>

有任何想法如何做到这一点

2 个答案:

答案 0 :(得分:1)

修改

如果我很清楚你想要做什么,用这个替换代码:

CustomizeDialog.java

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class CustomizeDialog extends Dialog {

    Button okButton;
    ImageView imageAnim = null;

    Bitmap bm;

    Context ctx;

    AnimationDrawable frameAnimation;

    public CustomizeDialog(Context context) {
        super(context);     

        ctx = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);     


        WindowManager.LayoutParams params = getWindow().getAttributes();  
        params.x = 30;  
        params.height = 500;  
        params.width = 500;  
        params.y = 50;  

        getWindow().setAttributes(params);  

        setContentView(R.layout.test);

        frameAnimation =  new AnimationDrawable(); 

        frameAnimation.addFrame(ctx.getResources().getDrawable(R.drawable.fb1), 1000);
        frameAnimation.addFrame(ctx.getResources().getDrawable(R.drawable.fb2), 1000);
        frameAnimation.addFrame(ctx.getResources().getDrawable(R.drawable.fb3), 1000);
        frameAnimation.addFrame(ctx.getResources().getDrawable(R.drawable.fb4), 1000);

        // set false for loop animation
        frameAnimation.setOneShot(false);

        imageAnim = (ImageView) findViewById(R.id.img); 
        imageAnim.setBackgroundDrawable(frameAnimation);

        imageAnim.post(new Starter()); 

    }

    class Starter implements Runnable { 

        public void run() { 
            frameAnimation.start(); 
        } 

    } 
}

text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gallery animated" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="154dp"
        android:layout_height="217dp" />

</LinearLayout>

此代码显示一个对话框,其中每秒加载4个图像(循环)。有用。 顺便说一下,你的代码存在很多问题。

1)首先,你必须在setContentView之后而不是之前将变量引用放到xml布局中!

2)当您执行 AnimationDrawable frameAnimation =(AnimationDrawable)img.getBackground() AnimationDrawable 强制转换为 BitmapDrawable p>

3)我不明白为什么你试图用NULL位图变量设置 img.setImageBitmap(bm) bm 从未设置过)。

4)你必须在onCreate方法中初始化contentView,而不是在CustomDialog构造函数中初始化。

5)您必须使用Thread来不停止UI加载。如果将动画循环放在代码对话框创建中,则可以停止对话并显示不正确。

希望这能帮到你

答案 1 :(得分:0)

尝试在CustomizeDialog类中实现一个更改图像的静态方法,当您想要更改图片时,只需调用CustomizeDialog.thatStaticMethod()。这样就是改变UI的原始类。