Android一种字幕效果的实现

参考链接

http://www.cnblogs.com/vaiyanzi/archive/2011/12/06/2277791.html

近期想学一下自定义View就想实现下字幕效果,在网上找了下,然后优化了下效果。

效果图




关键代码:

CaptionsTextView就是自定义的字幕View,单单实现功能,里面有一些细节需要调节



public class CaptionsTextView extends TextView{
    private int index=0;

    private float middleY;
    private float mX;
    private Paint mPaint;
    private Paint mPaint1,mPaint2,mPaint3,mPaint4;
    private Paint[] paints=new Paint[5];
    private List<Sentence> list;

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            invalidate();
        }
    };


    public CaptionsTextView(Context context) {
        super(context);
        init();
    }

    public CaptionsTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CaptionsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFocusable(true);
        if(list==null){
            list=new ArrayList<Sentence>();
            Sentence sen=new Sentence(0,"暂时没有通知公告");
            list.add(0, sen);
        }
        for (int i=0;i<5;i++){
            Paint paint=new Paint();
            paint.setAntiAlias(true);
            paint.setTextSize(20+i*3);
            paint.setColor(Color.BLACK);
            //水平方向拉伸两倍
            paint.setTextScaleX(1.5f);
            //设置字体水平倾斜度,普通斜体字是-0.25,可见往右斜
            paint.setTextSkewX((float) -0.25);
            paint.setTypeface(Typeface.SERIF);
            paint.setTextAlign(Paint.Align.CENTER);
            paints[i]=paint;

        }
        /*
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(16);
        mPaint.setColor(Color.BLACK);
        mPaint.setTypeface(Typeface.SERIF);

        mPaint1 = new Paint();
        mPaint1.setAntiAlias(true);
        mPaint1.setTextSize(17);
        mPaint1.setColor(Color.BLACK);
        mPaint1.setTypeface(Typeface.SERIF);

        mPaint2 = new Paint();
        mPaint2.setAntiAlias(true);
        mPaint2.setTextSize(18);
        mPaint2.setColor(Color.BLACK);
        mPaint2.setTypeface(Typeface.SERIF);

        mPaint3 = new Paint();
        mPaint3.setAntiAlias(true);
        mPaint3.setTextSize(19);
        mPaint3.setColor(Color.BLACK);
        mPaint3.setTypeface(Typeface.SERIF);

        mPaint4 = new Paint();
        mPaint4.setAntiAlias(true);
        mPaint4.setTextSize(20);
        mPaint4.setColor(Color.BLACK);
        mPaint4.setTypeface(Typeface.SERIF);
        */

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size=list.size();
        if (index==-1||index==size)
            return;
        float bottomY=middleY+2*40;
        canvas.drawText(list.get(index).getName(),mX,bottomY,paints[4]);
        float tempY=0;
        /*for (int i=index+1;i<size;i++){
            tempY=middleY+(i-index)*40;
            canvas.drawText(list.get(i).getName(),mX,tempY,paints[i%5]);

            if (i-index==2){
                break;
            }
        }*/

        for (int i=index-1;i>=0;i--){
            tempY=bottomY-(index-i)*70;
            canvas.drawText(list.get(i).getName(),mX,tempY,paints[4+i-index]);

            if (index-i==4){
                break;
            }
        }


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mX=w*0.5f;
        middleY=h*0.5f;
    }

    public void setList(List<Sentence> data){
        this.list=data;
    }

    public void updateUI(){
        new Thread(new UpdateThread()).start();
    }



    class UpdateThread implements Runnable{

        long time = 1000; // 开始 的时间,不能为零,否则前面几句歌词没有显示出来
        int i=0;

        @Override
        public void run() {
            while (true){
                if (index==list.size())
                    return;
                mHandler.sendEmptyMessage(1);
                try {
                    Thread.sleep(time);
                    index++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

使用:

public class CaptionViewActivity extends Activity{

    CaptionsTextView captionsTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_captions_view);
        captionsTextView = (CaptionsTextView) findViewById(R.id.captions_view);
        List lst=new ArrayList<Sentence>();
        for(int i=0;i<50;i++){
            if(i%2==0){
                Sentence sen=new Sentence(i,i+"、金球奖三甲揭晓 C罗梅西哈维入围 ");
                lst.add(i, sen);
            }else{
                Sentence sen=new Sentence(i,i+"、公牛欲用三大主力换魔兽????");
                lst.add(i, sen);
            }
        }
        //给View传递数据
        captionsTextView.setList(lst);
        //更新View
        captionsTextView.updateUI();
    }
}

布局:

<com.example.first.view.CaptionsTextView
    android:id="@+id/captions_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值