Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在imageview上应用动画_Android - Fatal编程技术网

Android 如何在imageview上应用动画

Android 如何在imageview上应用动画,android,Android,我写了这段代码,但它给出了致命的异常。因此,我处理了所有异常,这是什么原因。以及如何在imageview上应用动画 主要活动 public class AnimViewActivity extends Activity { /** Called when the activity is first created. */ Button b; ImageView iv; @Override public void onCreate(Bundle savedIn

我写了这段代码,但它给出了致命的异常。因此,我处理了所有异常,这是什么原因。以及如何在imageview上应用动画

主要活动

public class AnimViewActivity extends Activity {
    /** Called when the activity is first created. */
   Button b;
   ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.but);
        try{
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv=(ImageView)findViewById(R.id.im);
                iv.setVisibility(ImageView.VISIBLE);
                iv.setBackgroundResource(R.drawable.ic_launcher);
                iv.startAnimation(new Anmatam());
            }
        });
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
动画

public class Anmatam extends Animation {
float centerx,centery;
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        // TODO Auto-generated method stub

        super.applyTransformation(interpolatedTime, t);
        try{
        Matrix mt=t.getMatrix();
        mt.setScale(interpolatedTime, interpolatedTime);
        mt.preTranslate(-centerx,-centery);
        mt.postTranslate(centerx, centery);
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        // TODO Auto-generated method stub
        super.initialize(width, height, parentWidth, parentHeight);
        try{
        centerx=width/0.2f;
        centery=height/0.2f;
        setDuration(2000);
        setFillAfter(true);
        setInterpolator(new LinearInterpolator());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

}
清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.net.pro"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AnimViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Anmatam">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            </activity>
    </application>

</manifest>

您没有在此处捕获潜在的
NullPointerException
:iv.setVisibility(ImageView.VISIBLE)

如果在前一行未找到
ImageView
,则可能发生此情况