摘要:本文将带你了解Android应用开发之图形与动画在Android中的实现,希望本文对大家学Android有所帮助
本文将带你了解Android应用开发之图形与动画在Android中的实现,希望本文对大家学Android有所帮助
1、简单图形的绘制
Java代码 canvas.drawColor(Color.BLUE); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawRect(10, 10, 110, 110, paint); canvas.drawText("This is text", 10, 130, paint); RectF rf1 = new RectF(10, 130, 110, 230); canvas.drawArc(rf1, 0, 45, true, paint); canvas.drawLine(150, 10, 250, 110, paint); RectF rf2 = new RectF(150, 130, 250, 230); canvas.drawOval(rf2, paint); canvas.drawColor(Color.BLUE);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(10, 10, 110, 110, paint);
canvas.drawText("This is text", 10, 130, paint);
RectF rf1 = new RectF(10, 130, 110, 230);
canvas.drawArc(rf1, 0, 45, true, paint);
canvas.drawLine(150, 10, 250, 110, paint);
RectF rf2 = new RectF(150, 130, 250, 230);
canvas.drawOval(rf2, paint);
2、自定义动画的播放
Android中主要有两种动画模式,一种是tweenedanimation(渐变动画),即通过对场景的对象不断做图像变换产生动画效果;另一种是framebyframe(帧动画),即按照顺序播放事先配置好的动画帧。下面主要是渐变动画的应用。
首先创建动画文件donghuaanim.xml:
Xml代码 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="//schemas.android.com/apk/res/android"> <!-- 透明度变换 --> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000"/> <!-- 尺寸变换 --> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="3000"/> <!-- 位置变换 --> <translate android:fromXDelta="30" android:toXDelta="0" android:fromYDelta="30" android:toYDelta="50" android:duration="4000"/> <!-- 旋转变换 --> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="5000"/> </set> <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="//schemas.android.com/apk/res/android">
<!-- 透明度变换 -->
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"/>
<!-- 尺寸变换 -->
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="3000"/>
<!-- 位置变换 -->
<translate
android:fromXDelta="30"
android:toXDelta="0"
android:fromYDelta="30"
android:toYDelta="50"
android:duration="4000"/>
<!-- 旋转变换 -->
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"/>
</set>
添加布局文件donghua_xml.xml:
Xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="//schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:src="@mipmap/image"/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:src="@mipmap/image"/>
</LinearLayout>
添加DonghuaActivity.java文件:
Java代码 package xiao.fuyan.testapp; import android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; /** * Created by xiao on 2017/1/10. */ public class DonghuaActivity extends Activity { Animation animation; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.donghua_xml); animation = AnimationUtils.loadAnimation(this, R.anim.donghuaanim); imageView = (ImageView) findViewById(R.id.imageView); imageView.startAnimation(animation); } } package xiao.fuyan.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by xiao on 2017/1/10.
*/
public class DonghuaActivity extends Activity {
Animation animation;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.donghua_xml);
animation = AnimationUtils.loadAnimation(this, R.anim.donghuaanim);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.startAnimation(animation);
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号