走馬灯実現の3つの方法
5442 ワード
走馬灯を実現する3つの方法は、それぞれ直接コントロールを使って、HorizontalScrrollViewをカスタマイズし、TextViewをカスタマイズすることです.
一、直接コントロールを使う:
長所:簡単に使う
短所:拡張性が悪い
使用:XMLファイルに直接下記のコードを追加すればいいです.
二、HorizontalScrrollViewをカスタマイズする.
水平スライドのSrollViewを用いて実現した.
使用:まずHorizontalScrrollViewコントロールをカスタマイズし、XMLファイルで使用すればいいです.
1.ユーザー定義のコントロールコードは以下の通りです.
三.カスタムTextViewを使って実現する:
使用:まずTextViewコントロールをカスタマイズし、XMLファイルで使用すればいいです.
1.ユーザー定義のコントロールコードは以下の通りです.
大まかに整理して、足りないところを指摘してください.
一、直接コントロールを使う:
長所:簡単に使う
短所:拡張性が悪い
使用:XMLファイルに直接下記のコードを追加すればいいです.
二、HorizontalScrrollViewをカスタマイズする.
水平スライドのSrollViewを用いて実現した.
使用:まずHorizontalScrrollViewコントロールをカスタマイズし、XMLファイルで使用すればいいです.
1.ユーザー定義のコントロールコードは以下の通りです.
public class HorizontalScorllTextView extends HorizontalScrollView implements Runnable{
int currentScrollX = 0;//
TextView tv;
public HorizontalScorllTextView(Context context) {
super(context);
initView(context);
}
public HorizontalScorllTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public HorizontalScorllTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
void initView(Context context){
View v = LayoutInflater.from(context).inflate(R.layout.scroll_layout, null);
tv = (TextView)v.findViewById(R.id.tv_video_name);
this.addView(v);
}
public void setText(String text){
tv.setText(text);
startScroll();
}
private void startScroll(){
this.removeCallbacks(this);
post(this);
}
@Override
public void run() {
currentScrollX ++;//
scrollTo(currentScrollX, 0);
if (currentScrollX >= tv.getWidth()) {
scrollTo(0, 0);
currentScrollX = 0;
}
postDelayed(this, 50);
}
}
2.XMLで使用する場合は以下の通りです.
三.カスタムTextViewを使って実現する:
使用:まずTextViewコントロールをカスタマイズし、XMLファイルで使用すればいいです.
1.ユーザー定義のコントロールコードは以下の通りです.
public class MarqueeTextViewNew extends TextView implements Runnable {
private int screenWidth = 80;
private int currentScrollX = -screenWidth;//
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
public MarqueeTextViewNew(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTextViewNew(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextViewNew(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {//
getTextWidth();
isMeasure = true;
}
}
/**
*
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
// currentScrollX -= 1;// , :
currentScrollX += 1;// :
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (currentScrollX > textWidth) {
currentScrollX = -screenWidth;
}
// if (getScrollX() <= -(this.getWidth())) {
// scrollTo(textWidth, 0);
// currentScrollX = textWidth;
// }
postDelayed(this, 5);
}
//
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
//
public void stopScroll() {
isStop = true;
}
//
public void startFor0() {
currentScrollX = 0;
startScroll();
}
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(text, type);
startScroll();
}
@Override
public void destroyDrawingCache() {
// TODO Auto-generated method stub
super.destroyDrawingCache();
}
}
2.XMLでの使用は以下の通りです.
大まかに整理して、足りないところを指摘してください.