答案 0 :(得分:23)
创建自定义动画的最灵活(也很简单)方法是扩展Animation
类。
一般来说:
setDuration()
方法设置动画的持续时间。setInterpolator()
为动画设置插补器(例如,您可以使用LinearInterpolator
或AccelerateInterpolator
等。)applyTransformation
方法。在这里,我们感兴趣的是interpolatedTime
变量,它在0.0和1.0之间变化,代表你的动画进度。以下是一个示例(我正在使用此类来更改Bitmap
的设置。Bitmap
本身是用draw
方法绘制的:
public class SlideAnimation extends Animation {
private static final float SPEED = 0.5f;
private float mStart;
private float mEnd;
public SlideAnimation(float fromX, float toX) {
mStart = fromX;
mEnd = toX;
setInterpolator(new LinearInterpolator());
float duration = Math.abs(mEnd - mStart) / SPEED;
setDuration((long) duration);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float offset = (mEnd - mStart) * interpolatedTime + mStart;
mOffset = (int) offset;
postInvalidate();
}
}
您也可以使用View
修改Transformation#getMatrix()
。
<强>更新强>
如果您使用的是Android Animator框架(或兼容性实现 - NineOldAndroids
),您可以为自定义View
属性声明setter和getter,并直接为其设置动画。这是另一个例子:
public class MyView extends View {
private int propertyName = 50;
/* your code */
public int getPropertyName() {
return propertyName;
}
public void setPropertyName(int propertyName) {
this.propertyName = propertyName;
}
/*
There is no need to declare method for your animation, you
can, of course, freely do it outside of this class. I'm including code
here just for simplicity of answer.
*/
public void animateProperty() {
ObjectAnimator.ofInt(this, "propertyName", 123).start();
}
}
答案 1 :(得分:3)
Animation animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.exp1), 50);
animation.addFrame(getResources().getDrawable(R.drawable.exp2), 50);
animation.addFrame(getResources().getDrawable(R.drawable.exp3), 50);
animation.addFrame(getResources().getDrawable(R.drawable.exp4), 50);
animation.addFrame(getResources().getDrawable(R.drawable.exp5), 50);
animation.addFrame(getResources().getDrawable(R.drawable.exp6), 50);
这是我用来在onCreate()中生成自定义的逐帧动画的代码。
之后我需要启动动画,但必须在UI线程内完成。因此我使用了Runnable。
class Starter implements Runnable {
public void run() {
animation.stop();
animation.start();
}
}
我使用ImageView的.post()方法从onClick()启动Runnable:
((ImageView) findViewById(R.id.ImageToAnimateOnClicking)).post(new Starter());
答案 2 :(得分:1)
我假设您将每个帧创建为位图,然后直接将其传递给动画,而不是从资源中获取Drawable。
Bitmap bm = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_888);
Canvas c = new Canvas(bm);
.... Draw to bitmap
animation.addFrame(bm,50)
.... repeat for all frames you wish to add.
答案 3 :(得分:1)
当我用另一个片段替换Fragment时,我的自定义动画:
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="1000"/>
XML slide_in_right:
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="1000"/>
XML slide_out_left:
Option Explicit
Sub getOTC()
'Dim strPattern As String: strPattern = "^[0-9]{1,2}"
Dim DataSheet As Worksheet
Dim TransferSheet As Worksheet
Dim DataRange As Range
Dim CheckRange As Range
Dim R03 As Range
Dim CopyRange As Range
Dim PasteRange As Range
Dim regexp As Object
'Dim regex As New VBScript_RegExp_55.regexp
Dim strInput As String
Dim strPattern As String
Dim rcell As Range
strPattern = "([a-z])"
'making sure that data sheet exist ( thus will tell (vaidation))
If Not DoesSheetExist("Values", ThisWorkbook) Then
MsgBox ("No Sheet Name ""Values"" found!")
Exit Sub
End If
Set DataSheet = ThisWorkbook.Worksheets("Values")
Set DataRange = DataSheet.Range("A2:AI18254")
Set CheckRange = DataSheet.Range("H2:H18254")
'using regexp to get the value with other string value behind
Set regexp = CreateObject("vbscript.regexp")
With regexp
.Global = False
.MultiLine = False
.ignoreCase = True
.Pattern = strPattern
End With
For Each rcell In CheckRange.Cells
If strPattern <> "" Then
strInput = rcell.Value
If regexp.test(strInput) Then
MsgBox rcell & "Matched in Cell" & CheckRange.Address
Else
MsgBox " Could not find ""T04"" in the colum!"
End If
Exit Sub
End If
'a
If DoesSheetExist("Products Sales", ThisWorkbook) Then
MsgBox ("Dude, ""Products Sales"" sheet exist its goin to be deleted")
Set TransferSheet = Worksheets("Products Sales")
TransferSheet.Delete
End If
' this one is to create the transferSheet
Set TransferSheet = Worksheets.Add
TransferSheet.Name = "Products Sales"
CopyRange.Copy
TransferSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
Next
End Sub
答案 4 :(得分:0)
您可以将四种类型的动画添加到自定义视图中。
这是一个blog post,详细解释了每一个。
完成创建动画后,只需使用以下代码将该自定义动画添加到视图中。
findById(R.id.element).startAnimation(AnimationUtils.loadAnimation(this, R.anim.custom_animation));
答案 5 :(得分:-1)
除了在XML中定义补间动画外,您还可以定义逐帧动画(存储在res / drawable中)。
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/frame1" android:duration="300" />
<item android:drawable="@drawable/frame2" android:duration="300" />
<item android:drawable="@drawable/frame3" android:duration="300" />
</animation-list>
通过setBackgroundResource将动画设置为View background。
如果您想要做一些更复杂的事情,请查看Canvas课程。请参阅有关如何draw with Canvas的简介。