2014年1月9日 星期四

利用TextView實作橫向跑馬燈

利用TextView實作橫向跑馬燈



 


前天同事實作出利用TextView實作橫向跑馬燈,覺得很有趣,趕緊備份起來,歡迎有興趣的同好,一起來C/P。


 









package com.example.framelayout_textview_marquee;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;

///////////////////////////////////////
import android.view.View;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils.TruncateAt;
import android.text.method.ScrollingMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
public class MainActivity extends Activity {
FrameLayout FrameLayout1;
DisplayMetrics metrics;
VideoView VideoView1;
TextView TextView1;
int width,height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);

/////////////////////////////////////////
FrameLayout1 = new FrameLayout(this);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);

////////////////////////////////////////
TextView1=new TextView(this);
FrameLayout.LayoutParams layoutParams_TextView = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels/8);
layoutParams_TextView.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_TextView.leftMargin = 0;//X
layoutParams_TextView.topMargin = 0;//Y

TextView1.setLayoutParams(layoutParams_TextView);
//*
TextView1.setText(" 跑馬燈空白");//350
TextView1.setSingleLine();
TextView1.setEllipsize(TruncateAt.MARQUEE);
TextView1.setSelected(true);
//*/

FrameLayout1.addView(TextView1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


沒有留言:

張貼留言