传统的实现跑马灯效果,要设置textview一些属性,还要重写textview的onFouce()方法,但是传统的跑马灯效果的速度不好控制,今天就不使用传统的方式实现,
布局文件:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.marqueetext.MainActivity"> <com.example.marqueetext.MarqueeText android:layout_width="match_parent" android:layout_height="40px" android:background="#ff00ff" android:layout_marginTop="100px" /> </RelativeLayout>跑马灯自定义view
package com.example.marqueetext; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.WindowManager; /** * Created by Adminis on 2016/8/26. */ public class MarqueeText extends View { private static final String TAG ="MarqueeText" ; private int x = 0;// private int screenWidth; private int textWidth; private boolean isMeasure; private Paint mPaint; private String str = "晚上6点,银海国际酒店的三楼,600多平方的伊甸园里,布置简约而又充满的年轻点气息"; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==1){ x-=5; if(Math.abs(x)>=textWidth){ x=screenWidth; } invalidate(); mHandler.sendEmptyMessageDelayed(1,50); } } }; public MarqueeText(Context context) { this(context,null); } public MarqueeText(Context context, AttributeSet attrs) { this(context, attrs,0); } public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.WHITE); mPaint.setTextSize(20); WindowManager wm = ((MainActivity)context).getWindowManager(); screenWidth = wm.getDefaultDisplay().getWidth(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(!isMeasure){ textWidth = (int) mPaint.measureText(str); isMeasure = true; } canvas.drawText(str,x,25,mPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(screenWidth,dip2px(getContext(),40)); } @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); if(hasWindowFocus){ mHandler.sendEmptyMessage(1); }else{ mHandler.removeCallbacks(null); } } public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }效果图:
这个在模拟器上看起来有点卡,但是我在真机上是没有这个问题的!