- Bitmap
- isRecycle() 判斷對象是否回收
- recycle()回收自己
- createBitmap(source,x,y,width,height)從原位圖source,從x,y 開始挖出width,height 并建立新的bitmap對象
- BitmapDrawable裡面封裝的圖檔就是Bitmap
- BitmapDrawable drawable = new BitmapDrawable();
- 拿到Bitmap對象 Bitmap bitmap = drawable.getBitmap();
- BitmapFactory
- decodeByteArray() 從位元組數組解析Bitmap
- decodeFile(String path)從檔案解析Bitmap
- decodeStream(InputStream is)從輸入流解析建立Bitmap
- 幀動畫
- FrameAnimation
- 多張圖檔快速切換,形成動畫效果
- drawable檔案夾不放圖檔,隻放資源檔案
- ImageView顯示圖檔可以設定内容(src),也可以設定背景(background)
- 步驟
- 在drawable下建立資源檔案
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/g1" android:duration="200" /> <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> <item android:drawable="@drawable/g4" android:duration="200" /> <item android:drawable="@drawable/g5" android:duration="200" /> <item android:drawable="@drawable/g6" android:duration="400" /> <item android:drawable="@drawable/g7" android:duration="400" /> <item android:drawable="@drawable/g8" android:duration="400" /> <item android:drawable="@drawable/g9" android:duration="400" /> </animation-list>
- 在Java代碼中調用
package cqupt.com.frame;
import android.graphics.drawable.AnimationDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView; private AnimationDrawable animation;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.iv_image); imageView.setBackgroundResource(R.drawable.frame_animation); animation = (AnimationDrawable) imageView.getBackground(); }
public void startAnistartAnimation(View view) { animation.start(); } }
- 補間動畫
- TraslateAnimation
iv = (ImageView) findViewById(R.id.iv) TranslateAnimation ta = new TranslateAnimation(-100,100,0,0);
ta.setDuration(2000); iv.startAnimation(ta);
* -100:表示動畫的水準方向的初始坐标 * iv原始x -100 * 100:表示動畫的水準方向的結束坐标 * iv的原始x + 100
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,-1.5f,Animation.RELATIVE_TO_SELF,1.5f, Animation.RELATIVE_TO_SELF,-1.5f,Animation.RELATIVE_TO_SELF,1.5f);
ta.setDuration(2000); iv.startAnimation(ta);
* -1.5f:表示動畫的水準方向的初始坐标 * iv的原始x - iv寬度 * 1.5 * 1.5f:表示動畫的水準方向的結束坐标 * iv的原始x + iv寬度 * 1.5
屬性動畫的位移:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv,"translationX",-100,100); objectAnimator.setDuration(2000); objectAnimator.start();
利用資源檔案建立動畫: 1.在anima檔案下,建立tanslate_animation檔案
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="-100" android:fromYDelta="0" android:shareInterpolator="false" android:toXDelta="100" android:toYDelta="0" /> 2. 在Java檔案中加載:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.tanslate_animation); iv.startAnimation(animation);
- ScaleAnimation
ScaleAnimation sa = new ScaleAnimation(0.5f, 1, 0.5f, 1,iv.getWidth()/2,iv.getHeight()/2); sa.setDuration(2000); iv.startAnimation(sa);
0.5f, 1, 0.5f, 1 比例 iv.getWidth()/2,iv.getHeight()/2以左上點為基準
屬性動畫:
// iv.setScaleX(); ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"scaleX",0.5f,1); oa.setDuration(2000); oa.start(); xml資源檔案: res/anima資源檔案
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.5" android:toXScale="0.5" android:pivotX="0" android:pivotY="0" android:fromYScale="1" android:toYScale="1" />
java檔案中加載:
Animation am = AnimationUtils.loadAnimation(this, R.anim.scale_animation); iv.startAnimation(am);
- AlphaAnimation
AlphaAnimation aa = new AlphaAnimation(.1f,1); aa.setDuration(2000); iv.startAnimation(aa);
- RotateAnimation
RotateAnimation ra = new RotateAnimation(45, 360,iv.getWidth()/2,iv.getHeight()/2); ra.setDuration(2000); iv.startAnimation(ra);
- AnimationSet 動畫集
AnimationSet as = new AnimationSet(false); as.addAnimation(ta); as.addAnimation(sa); as.addAnimation(aa); iv.startAnimation(as); 在資源檔案中實作:
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.5" android:toXScale="0.5" android:pivotX="0" android:pivotY="0" android:fromYScale="1" android:toYScale="1" />
在Java檔案中加載:
Animation as = AnimationUtils.loadAnimation(this, R.anim.animation_set); iv.startAnimation(as);
- 屬性動畫
- ValueAnimation
用于沒有set方法,設定動畫 步驟: 一 通過ValueAnimation的ofInt(), ofFloat(),ofObject()靜态方法建立ValueAnimation 二 調ValueAnimation的方法設定時間,插入器,重複次數 三 addUpdateListener注冊監聽器 四 valueAnimator.getAnimatedValue()拿到漸變值使用 private void toggle() { if(mIsOpen){ //關閉 int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);//0 final int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);//0 desc.measure(width,height);//等效measure(0,0) descHeight = desc.getMeasuredHeight(); // Toast.makeText(UIUtils.getContext(), ""+descHeight, Toast.LENGTH_SHORT).show(); //播放動畫
int start = descHeight; int end = 0; doAnimator(start, end); }else{ //打開 int start = 0; int end = descHeight; doAnimator(start,end); } mIsOpen = !mIsOpen; }
private void doAnimator(int start, int end) { ValueAnimator animator = ValueAnimator.ofInt(start,end); animator.setDuration(500); animator.start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int heightTemp = (int) valueAnimator.getAnimatedValue(); ViewGroup.LayoutParams params = desc.getLayoutParams(); params.height = heightTemp; desc.setLayoutParams(params); } });
- ObjectAnimation(ValueAnimation的子類)
步驟 通過ObjectAnimation的ofInt(), ofFloat(),ofObject()靜态方法建立ValueAnimation 調ObjectAnimation的方法設定時間,插入器,重複次數 直接start() 一.
ObjectAnimator imageAnimal = ObjectAnimator.ofFloat(image, "translationX", 0, 720, 360, 0); imageAnimal.setDuration(3000); imageAnimal.start(); 二 . 較為特殊的一個:
setBackgroundColor(Color); ObjectAnimator tvAnimal = ObjectAnimator.ofObject(tv,"backgroundColor",new ArgbEvaluator(),Color.RED,Color.BLACK,Color.GREEN); tvAnimal.setDuration(3000); tvAnimal.setRepeatCount(ValueAnimator.INFINITE); tvAnimal.setRepeatMode(ValueAnimator.REVERSE); tvAnimal.start(); x
- 通過xml檔案建立動畫
- 補間動畫放在res/anima 檔案,屬性動畫放在res/animator下
- 使用AnimationUtil.loadAnimation()
- 相應的對象調用startAnimaiton()
- ObjectAnimation對應xml标簽是<ObjectAnimator
ValueAnimation對應的xml标簽是<animator>. 在Java檔案中加載動畫資源
// 加載動畫資源 ObjectAnimator colorAnim = (ObjectAnimator) AnimatorInflater .loadAnimator(MainActivity.this, R.animator.color_anim); colorAnim.setEvaluator(new ArgbEvaluator()); // 對該View本身應用屬性動畫 colorAnim.setTarget(this); // 開始指定動畫 colorAnim.start();