android中實現(xiàn)懸浮窗口并滾動
(2012-08-27 11:36:55)
因為項目需要最近研究了android中實現(xiàn)懸浮并滾動的效果,筆記如下。
首先要實現(xiàn)一個自己畫的滾動View.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AutoScroll extends TextView {
private float textLength = 0f;//文本長度
private float viewWidth = 0f;
private float step = 0f;//文字的橫坐標(biāo)
private float y = 0f;//文字的縱坐標(biāo)
private float temp_view_plus_text_length =
0.0f;//用于計算的臨時變量
private float temp_view_plus_two_text_length =
0.0f;//用于計算的臨時變量
public boolean isStarting = false;//是否開始滾動
private Paint paint = null;//繪圖樣式
private String text = "";//文本內(nèi)容
Canvas acanvas;
private Handler handler = new Handler()
{
@Override
public void
handleMessage(Message msg)
{
onDraw(acanvas);
}
};
public AutoScroll(Context context)
{
super(context);
// initView();
}
public AutoScroll(Context context, AttributeSet
attrs)
{
super(context, attrs);
// initView();
}
public AutoScroll(Context context, AttributeSet
attrs, int defStyle)
{
super(context, attrs,
defStyle);
// initView();
}
// public void onClick(View v) {
// if(isStarting)
//
stopScroll();
//
else
//
startScroll();
// }
// private void initView()
//
{
//
setOnClickListener(this);
//
}
public void init(WindowManager
windowManager)
{
paint = getPaint();
text = getText().toString();
textLength = paint.measureText(text);//textview中字?jǐn)?shù)的長度
viewWidth = getWidth();
if(viewWidth == 0)
{
if(windowManager != null)
{
Display display = windowManager.getDefaultDisplay();
viewWidth = display.getWidth();
} |