仿Twitter登陸移動背景效果

來源:互聯網
上載者:User

標籤:

有使用Twitter用戶端的朋友應該有發現在登入的時候,螢幕上方和下方的雲朵圖片是不斷移動著的,再加上Twitter那支可愛的小鳥在不停的動著,給人的感覺就好像是小鳥在飛翔一樣,感覺效果很不錯。

我也試著實現了一下,發現使用自訂的Drawable加上線程,還是比較容易實現的。下面是自訂Drawable的代碼,注釋已經寫的很詳細了。這裡是使用了三張圖片,使用一張圖就更簡單了,大家完全可以舉一反三,實現自己的動畫圖片效果。

package com.liuzhichao.dynamicdrawable;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.ColorFilter;import android.graphics.drawable.Drawable;import android.util.DisplayMetrics;import android.view.View;public class DynamicDrawable extends Drawable implements Runnable {    private static Bitmap mBottomBgBitmap;   //底部大圖      private static Bitmap mBottomFgBitmap;   // 底部小圖     private static Bitmap mTopBitmap;      //頂部雲圖片    private final int dispalyWidth;       //手機螢幕寬度    private final int dispalyHeight;     //手機螢幕高度    private float mBottomBgTop;             //頂部大圖顯示的位置    private float mBottomFgTop;            //頂部小圖顯示的位置        private int startX;                //頂部雲圖片和底部大圖移動位移變數    private int mBottomFgStartX;       //底部小圖移動位移變數    private View mView;    public DynamicDrawable(Activity activity, View mView) {        this.mView = mView;        if (mTopBitmap == null) {            mTopBitmap = BitmapFactory.decodeResource(activity.getResources(),                    R.drawable.cloud_top_bg);        }        if (mBottomBgBitmap == null) {            mBottomBgBitmap = BitmapFactory.decodeResource(                    activity.getResources(), R.drawable.cloud_bottom_bg);        }        if (mBottomFgBitmap == null) {            mBottomFgBitmap = BitmapFactory.decodeResource(                    activity.getResources(), R.drawable.cloud_bottom_fg);        }        //擷取螢幕高度和寬度        DisplayMetrics dm = new DisplayMetrics();        dm = activity.getResources().getDisplayMetrics();        this.dispalyHeight = dm.heightPixels;        this.dispalyWidth = dm.widthPixels;        //頂部圖片顯示的位置  = 螢幕高度 - 圖片自身高 - 標題高度        this.mBottomBgTop = dispalyHeight - mBottomBgBitmap.getHeight() - 100;        this.mBottomFgTop = dispalyHeight - mBottomFgBitmap.getHeight() - 100;        //開始移動圖片線程        new Thread(this).start();    }    @Override    public void draw(Canvas canvas) {        // drawBitmap (Bitmap bitmap, float left, float top, Paint paint)        canvas.drawBitmap(mTopBitmap, startX, 0, null);        canvas.drawBitmap(mBottomBgBitmap, startX, mBottomBgTop, null);        canvas.drawBitmap(mBottomFgBitmap, mBottomFgStartX, mBottomFgTop, null);    }    @Override    public int getOpacity() {        // TODO Auto-generated method stub        return 0;    }    @Override    public void setAlpha(int arg0) {        // TODO Auto-generated method stub    }    @Override    public void setColorFilter(ColorFilter arg0) {        // TODO Auto-generated method stub    }    @Override    public void run() {        while (!Thread.currentThread().isInterrupted()) {            //900 是圖片的長度            //如果移動偏量已經大於圖片的長度 - 螢幕寬度,則說明已經移動到了圖片的末端,如果繼續移動,則會顯示空白            //所以將startX重新賦予0,讓圖片重新開始移動            //但是這樣過渡效果會比較明顯,再切換的那一刹那,會明顯感覺圖片變化較大            if (startX + (900 - dispalyWidth) == 0) {                startX = 0;            } else {                startX -= 1;            }            //底部小圖的移動,            // -=2 是讓小圖移動的比大圖快,造成不同的視覺效果            if (mBottomFgStartX + (900 - dispalyWidth) == 0) {                mBottomFgStartX = 0;            } else {                mBottomFgStartX -= 2;            }            try {                //每 1/10秒移動一次                Thread.sleep(100L);            } catch (InterruptedException e) {                Thread.currentThread().interrupt();            }            //重新整理介面            mView.postInvalidate();        }    }}

然後在需要背景圖的地方,直接setBackgroundDrawable就可以了,下面的background是整個布局檔案的id。

private View background;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        background = findViewById(android.R.id.background);        background.setBackgroundDrawable(new DynamicDrawable(this, background));    }

布局檔案也還是貼出來吧:

<RelativeLayout 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"    android:id="@android:id/background" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:padding="@dimen/padding_medium"        android:text="@string/hello_world"        tools:context=".MainActivity" /></RelativeLayout>

還是截張圖吧,雖然看不到動畫效果。

 

本文轉自:http://liuzhichao.com/p/659.html

仿Twitter登陸移動背景效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.