现在的位置: 首页 > 综合 > 正文

配置文件实现渐变

2018年03月31日 ⁄ 综合 ⁄ 共 2027字 ⁄ 字号 评论关闭

 

在Android中所有定义好的XML文件都要求保存在res/anim文件夹之中。

 

 

 

准备一张名为picture的图片。

 

 

在main.xml中:

 

<LinearLayout

    android:id="@+id/group"

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <ImageView

      android:id="@+id/mlyw"

      android:layout_marginTop="8dp"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:src="@drawable/picture"/>

  <Button

      android:id="@+id/but"

      android:layout_width="100dp"

      android:layout_height="40dp"

      android:layout_marginTop="80dp"

      android:background="#3399ff"

      android:textColor="#ffffff"

      android:text="开始渐变"/>

</LinearLayout>

 

 

 

 

 

 

在MyAnimationDemo.java中:

 

package com.li.animation;

 

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.ImageView;

 

 

@SuppressLint({ "ParserError", "ParserError" })

public class MyAnimationDemo extends Activity {

  private ImageView mlyw = null;

  private Button but = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.mlyw = (ImageView)super.findViewById(R.id.mlyw);

        this.but = (Button)super.findViewById(R.id.but);

        this.but.setOnClickListener(new OnClickListenerImpl());

    }

    private class OnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       Animation anim = AnimationUtils.loadAnimation(

            MyAnimationDemo.this, R.anim.alpha);

       MyAnimationDemo.this.mlyw.startAnimation(anim);

     }

    }

}

 

 

 

 

 

在res/anim下新建alpha.xml:

 

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:duration="5000"/>

</set>

 

【上篇】
【下篇】

抱歉!评论已关闭.