在android的xml和java代码中使用透明度动画
来源:互联网 发布:淘宝店铺介绍文艺句子 编辑:程序博客网 时间:2024/06/06 07:09
在xml中定义透明动画
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true" android:fromAlpha="1.0" android:toAlpha="0.3"></alpha>
不知道为什么,在我的AndroidStudio中,使用自定提示功能只提示开始透明度值和结束透明度值这两个属性,希望知道的朋友可以告诉我一下为什么,是不是我的AS设置有问题!
在Activity中通过使用AnimationUtils工具类来加载在xml中定义的动画,具体的代码如下所示:
package com.example.alpha;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.DisplayMetrics;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.view.animation.AlphaAnimation;import android.view.animation.AnimationUtils;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView)findViewById(R.id.image_view); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); final AlphaAnimation alpha = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { image.startAnimation(alpha); } }); setImageView(); } private void setImageView(){ float density = getResources().getDisplayMetrics().density; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test,options); int width = options.outWidth; int height = options.outHeight; int sample = 1; if (width>image.getWidth() || height>image.getHeight()){ sample = (int) Math.max(width/(200*density),height/(200*density)); } options.inSampleSize = sample; options.inJustDecodeBounds = false; mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test,options); image.setImageBitmap(mBitmap); }}
当然也可以在java中定义透明度动画(google考虑的还是非常周到的)
private AlphaAnimation getAlphaAnimation(){ AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f); alphaAnimation.setDuration(3000); alphaAnimation.setFillAfter(true); return alphaAnimation; }
注:其实通过查看AlphaAnimation的源代码,是可以看出在xml中使用alpha动画时能够设置的属性名称的!
布局文件代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.alpha.MainActivity" tools:showIn="@layout/activity_main"> <ImageView android:id="@+id/image_view" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" /></RelativeLayout>
将android:fillAfter=”true”这个属性设置为false可以让动画结束后恢复到初始状态,还有其他有意思的属性,感兴趣的朋友可以自己试一下!
这是我的个人公众号:希望您可以关注一下,这将是对我最大的鼓励!
代码地址:
GitHub地址,后期所有与动画相关的代码都会存入这个GitHub仓库中
0 0
- 在android的xml和java代码中使用透明度动画
- Android 在Xml控件中对应的颜色透明度
- Android 在Xml控件中对应的颜色透明度
- 【Android 基础】Animation 动画介绍和实现【帧动画和Tween动画【在代码和xml中实现】】
- Android color(颜色) 在XML文件和java代码中
- Android在xml中设置组件风格(圆角,点击效果selector,边框,进度条风格,动画)TextView文字透明度
- 如何在android的XML和java代码中引用字符串常量
- 动画用xml和Java代码之间的简单转换
- Android初学习 - color在XML文件和java代码中的定义和使用
- 在代码中对窗体设置透明度灰度的方法
- Android中使用Java代码动画改变背景颜色
- 在xml中使用属性动画
- android中用java代码动态控制activity的大小,位置,透明度
- ModelImporter的使用、在代码中添加动画片段
- 在java中使用dom4j解析xml(示例代码)
- 在java中使用dom4j解析xml(示例代码)
- 在java中使用dom4j解析xml(示例代码)
- 在java中使用dom4j解析xml(示例代码)
- React 笔记4:Components and Props
- 台风路径模拟方法介绍
- LeetCode 61 Rotate List
- SQL
- java之集合13
- 在android的xml和java代码中使用透明度动画
- 抵制萨德抵制乐天--蠢货行为
- [Offer收割]编程练习赛3
- Microsoft AirSim 开发(一)平台的搭建
- LeetCode
- sqlplus 基本操作
- centos7 执行service network restart出现错误解决办法
- dp
- zbnet2