先看一种错误的写法:
自定义的Dialog布局customdialog.xml:
<?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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="@drawable/progress" />
</LinearLayout>
自定义的Dialog类,TransparentProgressDialog.java:
package com.example.circleprogressbar;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context) {
super(context, R.style.TransparentProgressDialog);
View view=LayoutInflater.from(context).inflate(R.layout.customdialog, null);
iv=(ImageView)view.findViewById(R.id.imageView1);
setContentView(view);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f,Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);
}
}
在这里
View view=LayoutInflater.from(context).inflate(R.layout.customdialog, null);
iv=(ImageView)view.findViewById(R.id.imageView1);
这两句是取不到值的,虽然Eclipse没有报错,但是仍然没有给iv赋值。
正确的写法如下TransparentProgressDialog.java:
package com.example.circleprogressbar;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context,LayoutInflater layoutInflater) {
super(context, R.style.TransparentProgressDialog);
View view=layoutInflater.inflate(R.layout.customdialog, null);
iv=(ImageView)view.findViewById(R.id.imageView1);
setContentView(view);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f,Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);
}
}
在Activity.java中这样创建一个TransparentProgressDialog:
TransparentProgressDialog pd = new TransparentProgressDialog(MainActivity.this,getLayoutInflater());
这样就正确的给iv赋值了。