當前位置:首頁 » 安卓系統 » androidgifdrawable

androidgifdrawable

發布時間: 2022-06-02 11:51:34

『壹』 安卓中如何添加顯示gif動態圖片

大家都知道,Android開發模擬器為了節省內存,一般不支持直接顯示gif圖片,即使你強制設置了,也只會顯示圖片的第一幀。看到網上也有許多的方法,來實現此功能,可都比較的繁瑣,需要修改android源代碼來實現或者用gif解析器來實現。在此文章中,這里教大家一種比較簡潔的一個方法,你可以把這個類當做是一種工具類。用的時候,直接搬到程序裡面,更改下圖片的資源,就可以非常輕松的顯示gif圖片了。


步驟1:看一下這個工具類的實例代碼:MyGifView.java

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Movie;

import android.util.AttributeSet;

import android.view.View;

public class MyGifView extends View{

private long movieStart;

private Movie movie;

//此處必須重寫該構造方法

public MyGifView(Context context,AttributeSet attributeSet) {

super(context,attributeSet);

//以文件流(InputStream)讀取進gif圖片資源

movie=Movie.decodeStream(getResources().openRawResource(R.drawable.keyboard));

}

@Override

protected void onDraw(Canvas canvas) {

long curTime=android.os.SystemClock.uptimeMillis();

//第一次播放

if (movieStart == 0) {

movieStart = curTime;

}

if (movie != null) {

int raction = movie.ration();

int relTime = (int) ((curTime-movieStart)%raction);

movie.setTime(relTime);

movie.draw(canvas, 0, 0);

//強制重繪

invalidate();

}

super.onDraw(canvas);

}

}

此工具類中,只做了2件事情。1,構造方法;2,重寫了onDraw()方法。大家以後用的話,只需拷貝此類到你的工程下即可起作用。


步驟2:布局文件代碼 activity_main.xml

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<TextView

android:text="====Gif圖片測試布局===="

android:layout_height="wrap_content"

android:layout_width="wrap_content"

/>

<com.example.showgifimage.MyGifView

android:id="@+id/iv"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_margin="20dp"

/>

</LinearLayout>

布局文件中,注意的是:標簽的設置 <com.example.showgifimage.MyGifView/>.

格式:包名+.類名


步驟3:主activity中調用

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

就這么輕松的把Gif圖片顯示了~~

『貳』 Android開發怎樣顯示gif格式的圖片

1、下載安裝AnimGIF Live Wallpaper,值得注意的是,安裝成功之後,在應用程序列表是無法找到它的圖標,找到方式看下圖:2、進入AnimGIF Live Wallpaper,點擊左下方的設置按鈕,選擇「Set GIF Image」按鈕即進入文件目錄瀏覽,找到需要設置為壁紙的GIF動圖,點擊設置壁紙即可。注意事項:1、GIF動圖體積不要過大,盡量不要超過1M,否則會出現卡頓、掉幀等播放問題;2、設置動圖為壁紙後,會導致耗電量的增加。

『叄』 有哪些常用或優秀的 Android 第三方類庫

Android開發第三方類庫分類:

  1. gifview 、 gifview2

    這兩個項目是同一人開發的,用於顯示gif動畫,純java解碼 ,gif圖片過大會產生OOM。

  2. android-gif-drawable

    一個也是用於解碼顯示gif的類庫,採用jni解碼,大GIF也不會產生oom,完美運行。

  3. PhotoView

    PhotoView 是一款擴展自Android ImageView ,支持通過單點/多點觸摸來進行圖片縮放的智能控制項。使用比較簡單,提供了一個名為PhotoView的控制項,可以直接像普通ImageView那樣使用。

    特性:支持單點/多點觸摸,即時縮放圖片;支持平滑滾動;在滑動父控制項下能夠運行良好;(例如:ViewPager)

不同的環境使用不同的類庫,不同的系統也是同樣的道理。

『肆』 Android 中的動畫有哪幾類,它們的特點和區別是什麼

Android3.0(即API Level11)前Android僅支持2種畫:別Frame Animation(逐幀畫)Tween Animation(補間畫)3.0Android支持種新畫系統稱:Property Animation(屬性畫)

、Frame Animation:(逐幀畫)

理解幀幀播放圖片利用眼視覺殘留原理給我帶畫覺原理GIF圖片、電影播放原理

1.定義逐幀畫比較簡單要使用元素定義所播放幀即

(1) android:oneshot 設置否僅播放

(2) android:drawable 設置每幀圖片

(3) android:ration 設置圖片間切換間隔

2.習慣AnimationDrawable設置ImageView背景

android:background=@anim/frame_anim

我java代碼獲取AnimationDrawable象

AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();

(需要注意AnimationDrawable默認播放調用其start()始播放stop停止播放)

3.面畫文件通xml文件配置喜歡通java代碼創建AnimationDrawable象通addFrame(Drawable frame, int ration)向畫添加幀start()

二、Tween Animation:(補間畫)

補間畫我需指定始、結束關鍵幀變化其幀由系統計算必自幀幀定義

1. Android使用Animation代表抽象畫包括四種類:AlphaAnimation(透明度畫)、ScaleAnimation(縮放畫)、TranslateAnimation(位移畫)、RotateAnimation(透明度畫)Android面允許java創建Animation類象般都採用畫資源文件定義畫界面與邏輯離

(set同定義畫起執行)

2. android:interpolator=@android:anim/linear_interpolator控制畫期間需要補入少幀簡單說控制畫速度些翻譯插值Interpolator幾種實現類:LinearInterpolator、AccelerateInterpolator、、CycleInterpolator、DecelerateInterpolator具體使用參考官API Demo

3. 定義anim文件我通AnimationUtils工具類載入載入功返Animation通ViewstartAnimation(anim)始執行畫

Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
//設置畫結束保留結束狀態
anim.setFillAfter(true);
//設置插值效
anim.setInterpolator(interpolator);
//view執行畫
view. startAnimation(anim);

三、Property Animation:(屬性畫)

屬性畫Android 3.0才引進直接更改我象屬性面提Tween Animation更改View繪畫效View真實屬性改變假設用Tween畫Button左邊移右邊論點擊移Button都沒反應點擊移前Button位置才反應Button位置屬性木改變Property Animation則直接改變View象屬性值讓我少做些處理工作提高效率與代碼讀性

(1)ValueAnimator:包含Property Animation畫所核功能畫間始、結束屬性值相應間屬性值計算等應用ValueAnimator兩步驟

1計算屬性值

2根據屬性值執行相應作改變象某屬性

我主第二步需要實現ValueAnimator.onUpdateListener介面介面函數onAnimationUpdate()要改變View象屬性事情該介面do

animation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//do your work
}
});

(2)ObjectAnimator:繼承自ValueAnimator要指定象及該象屬性屬性值計算完自設置該象相應屬性即完Property Animation全部兩步操作實際應用般都用ObjectAnimator改變某象某屬性用ObjectAnimator定限制要想使用ObjectAnimator應該滿足條件:

1.象應該setter函數:set(駝峰命名)

2面例像ofFloat類工場第參數象名第二屬性名面參數變參數values…參數設置值假定目值屬性值變化范圍前值目值獲前值該象要相應屬性getter:get

3getter其應返值類型應與相應setter參數類型致

ObjectAnimator oa=ObjectAnimator.ofFloat(tv, alpha, 0f, 1f);
oa.setDuration(3000);
oa.start();

滿足面條件我能乖乖使用ValueAnimator創建畫

(3)Animator.AnimatorListener:Animator設置畫監聽需要重寫面四

onAnimationStart()
onAnimationEnd()
onAnimationRepeat()
onAnimationCancel()

我實現AnimatorListenerAdapter處用定義想監聽事件用實現每函數卻定義空函數體:

anim.addListener(new AnimatorListenerAdapter() {
public void on AnimationEnd(Animator animation){
//do your work
}
});

(4)AnimationSet:組合畫共同工作

AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4)
bouncer.play(anim5).after(amin2);
animatorSet.start();

面代碼意思: 首先播放anim1;同播放anim2,anim3,anim4;播放anim5

(5)TimeInterplator:與Tweeninterpolator類似幾種

AccelerateInterpolator 加速始慢間加速

DecelerateInterpolator 減速始快減速

先加速減速始結束慢間加速

AnticipateInterpolator 反向 先向相反向改變段再加速播放

反向加彈先向相反向改變再加速播放超目值緩慢移至目值

BounceInterpolator 跳躍快目值值跳躍目值100面值能依8577708090100

CycleIinterpolator 循環畫循環定數值改變弦函數:Math.sin(2 * mCycles * Math.PI * input)

LinearInterpolator 線性線性均勻改變

OvershottInterpolator 彈超目值緩慢改變目值

TimeInterpolator 介面允許自定義interpolator幾都實現介面

(6)Keyframes:讓我定義除始結束外關鍵幀KeyFrame抽象類要通ofInt(),ofFloat(),ofObject()獲適KeyFrame通PropertyValuesHolder.ofKeyframe獲PropertyValuesHolder象:

Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(width, kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn, pvhRotation);
述代碼意思:設置btn象width屬性值使其:始 Width=400畫始1/4 Width=200畫始1/2 Width=400畫始3/4 Width=100畫結束 Width=500

(7)ViewPropertyAnimator:View同改變種屬性非推薦用種該類屬性畫進行優化合並些invalidate()減少刷新視圖且使用起非簡便要求API LEVEL 12即Android 3.1僅需要行代碼即完水平、豎直移

myView.animate().translationX(50f). translationY(100f);

(8)需要改變些屬性:

translationX,translationY: View相於原始位置偏移量

rotation,rotationX,rotationY: 旋轉rotation用於2D旋轉角度3D用兩

scaleX,scaleY: 縮放比

x,y: View終坐標Viewlefttop位置加translationXtranslationY

alpha: 透明度

四、自總結三種畫優缺點:

(1)Frame Animation(幀畫)主要用於播放幀幀准備圖片類似GIF圖片優點使用簡單便、缺點需要事先准備每幀圖片;

(2)Tween Animation(補間畫)僅需定義始與結束關鍵幀變化間幀由系統補優點用准備每幀缺點改變象繪制沒改變View本身屬性改變按鈕位置需要點擊原按鈕所位置才效

(3)Property Animation(屬性畫)3.0推畫優點使用簡單、降低實現復雜度、直接更改象屬性、幾乎適用於任何象僅非View類缺點需要3.0API支持限制較目前外源庫提供低版本支持

『伍』 android 如何向drawable導入圖片

復制那張圖片 然後選中你要復制進去的目錄然後ctrl+v就行了。

『陸』 怎樣讓android應用啟動gif動起來

1-把GifView.jar加入你的項目。

2-在xml中配置GifView的基本屬性,GifView繼承自View類,和Button、ImageView一樣是一個UI控制項。如:

view plain to clipboardprint <com.ant.liao.GifView android:id="@+id/gif2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" /$amp;>amp;$nbsp;
<com.ant.liao.GifView android:id="@+id/gif2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" />

3-在代碼中配置常用屬性:

view plain to clipboardprint // 從xml中得到GifView的句柄
gf1 = (GifView) findViewById(R.id.gif1);
// 設置Gif圖片源
gf1.setGifImage(R.drawable.gif1);
// 添加監聽器
gf1.setOnClickListener(this);
// 設置顯示的大小,拉伸或者壓縮
gf1.setShowDimension(300, 300);
// 設置載入方式:先載入後顯示、邊載入邊顯示、只顯示第一幀再顯示
gf1.setGifImageType(GifImageType.COVER);
// 從xml中得到GifView的句柄
gf1 = (GifView) findViewById(R.id.gif1);
// 設置Gif圖片源
gf1.setGifImage(R.drawable.gif1);
// 添加監聽器
gf1.setOnClickListener(this);
// 設置顯示的大小,拉伸或者壓縮
gf1.setShowDimension(300, 300);
// 設置載入方式:先載入後顯示、邊載入邊顯示、只顯示第一幀再顯示
gf1.setGifImageType(GifImageType.COVER);

GifView的Jar包共有四個類:

GifAction.java

觀察者類,監視GIF是否載入成功

GifFrame.java

裡面三個成員:當前圖片、延時、下張Frame的鏈接。

GifDecoder.java

解碼線程類

GifView.java

主類,包括常用方法,如GifView構造方法、設置圖片源、延遲、繪制等。

『柒』 android 判斷drawable資源文件是不是gif

png

熱點內容
資料庫優先 發布:2025-05-20 13:59:39 瀏覽:277
androidurl判斷 發布:2025-05-20 13:50:11 瀏覽:259
共享文件夾沒有啟動伺服器服務 發布:2025-05-20 13:43:38 瀏覽:804
天龍八部游戲怎麼切換伺服器 發布:2025-05-20 13:42:36 瀏覽:631
亞馬遜免費主機是什麼配置 發布:2025-05-20 13:40:10 瀏覽:696
存儲類型默認分配 發布:2025-05-20 13:32:26 瀏覽:663
qq信息加密 發布:2025-05-20 13:31:32 瀏覽:342
文件夾嗅探器foldersniffer 發布:2025-05-20 12:33:36 瀏覽:913
編譯裝入 發布:2025-05-20 12:32:48 瀏覽:563
萬勝壓縮機價格 發布:2025-05-20 12:20:00 瀏覽:988