android通過繪製遮罩實現逐行顯示資訊

來源:互聯網
上載者:User

前一篇講到通過開多個線程來啟動每個view 的動畫來實現頁面的逐條重新整理,開銷很大。這裡給出了一個比較合理的方案,就是在頁面上產生一個遮罩,通過遮罩的下移動畫來實現內容的逐步重新整理。

 

主畫面類:

 

package com.drawmask;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class DrawMask extends Activity implements OnClickListener {

    private LinearLayout reload;
    private View mask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 擷取螢幕寬高
        DisplayMetrics dm = new DisplayMetrics();
        dm = getApplicationContext().getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;

        RelativeLayout parent = (RelativeLayout) findViewById(R.id.RelativeLayout01);

        // 建立遮罩
        mask = new Mask(this, screenWidth, screenHeight);

        parent.addView(mask);

        startOpenAnimation();
       
        // 點擊螢幕重啟遮罩動畫
        reload = (LinearLayout) findViewById(R.id.reload);
        reload.setOnClickListener(this);

    }

    private void startOpenAnimation() {

        TranslateAnimation ta = new TranslateAnimation(0, 0, 0, 500);
        ta.setDuration(2000);
        ta.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation animation) {
                mask.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {

            }

        });
         mask.startAnimation(ta);
    }

    @Override
    public void onClick(View v) {

        startOpenAnimation();
    }
}

 

遮罩類:

package com.drawmask;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.view.View;

public class Mask extends View{

    private int width, higth;
    @Override
    protected void onDraw(Canvas canvas) {

        // 產生可以繪製透明漸層的矩形View的畫筆
        Paint paint = new Paint();
        Shader mShader=new LinearGradient(0,0,0,50,
                new int[]{Color.argb(0, 255, 255, 255),Color.WHITE},
                null,Shader.TileMode.CLAMP);
        paint.setShader(mShader);
       
        // 以繪製透明漸層的矩形View
        Rect rect = new Rect();
        rect.set(0, 0, width, higth);
        canvas.drawRect(rect, paint);
    }
   
    public Mask(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
   
    public Mask(Context context, int width, int higth) {
        super(context);
        this.higth = higth;
        this.width = width;
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.