Арабский язык не отображается, если при запуске в Android не объединены символы английского языка при переопределении TextView и прокрутки текста с помощью android.widget.Scroller.
Но если я добавляю какой-либо английский символ к арабской строке, он правильно показывает арабский язык.
Ниже приведен код ScrollTextView: -
import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;
public class ScrollTextView extends TextView {
public int widthoftxt;
public boolean isSmall;
int refScrolLen=230;
public static int screenwidth;
public int widthofscreen;
public String speed;
// scrolling feature
private Scroller mSlr;
//Ticker Text Length
public int strln;
//Ticker Direction
public String direc;
// milliseconds for a round of scrolling
private int mRndDuration = 10000;
// the X offset when paused
private int mXPaused = 0;
// whether it's being paused
private boolean mPaused = true;
/*
* constructor
*/
public ScrollTextView(Context context) {
this(context, null);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/**
* begin to scroll the text from the original position
*/
public void startScroll() {
// begin from the very right side
mXPaused = -1 * getWidth();
// assume it's paused
mPaused = true;
resumeScroll();
}
/**
* resume the scroll from the pausing point
*/
public void resumeScroll() {
if (!mPaused)
return;
// Do not know why it would not scroll sometimes
// if setHorizontallyScrolling is called in constructor.
setHorizontallyScrolling(true);
// use LinearInterpolator for steady scrolling
mSlr = new Scroller(this.getContext(), new LinearInterpolator());
setScroller(mSlr);
int scrollingLen = calculateScrollingLen();
int distance = scrollingLen - (getWidth() + mXPaused);
int duration = (new Double(mRndDuration * distance * 1.00000/ scrollingLen)).intValue();
int dur=0;
int sp=3000;
if(speed.equalsIgnoreCase("slow")){
sp=5000;
}
else if(speed.equalsIgnoreCase("medium")){
sp=3000;
}
else if(speed.equalsIgnoreCase("fast")){
sp=1000;
}
if(direc.equalsIgnoreCase("rtl")){
int temp=scrollingLen/refScrolLen;
dur=temp*sp;
setVisibility(VISIBLE);
//For LEFT
mSlr.startScroll(mXPaused, 0, scrollingLen, 0,dur);
}
if(direc.equalsIgnoreCase("ltr")){
int temp=(scrollingLen + screenwidth)/refScrolLen;
dur=temp*sp;
setVisibility(VISIBLE);
//For RIGHT
mSlr.startScroll(scrollingLen, 0, -(scrollingLen + screenwidth), 0, dur);
}
/*
* Parameters
startX Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
startY Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
dx Horizontal distance to travel. Positive numbers will scroll the content to the left.
dy Vertical distance to travel. Positive numbers will scroll the content up.
duration Duration of the scroll in milliseconds.
*/
mPaused = false;
}
/**
* calculate the scrolling length of the text in pixel
*
* @return the scrolling length in pixels
*/
private int calculateScrollingLen() {
TextPaint tp = getPaint();
Rect rect = new Rect();
String strTxt = getText().toString();
Log.e("LENGTH OF TEXT ",strTxt.length()+strTxt);
tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
int scrollingLen = rect.width() + getWidth();
rect = null;
return scrollingLen;
}
/**
* pause scrolling the text
*/
public void pauseScroll() {
if (null == mSlr)
return;
if (mPaused)
return;
mPaused = true;
// abortAnimation sets the current X to be the final X,
// and sets isFinished to be true
// so current position shall be saved
mXPaused = mSlr.getCurrX();
mSlr.abortAnimation();
}
@Override
/*
* override the computeScroll to restart scrolling when finished so as that
* the text is scrolled forever
*/
public void computeScroll() {
super.computeScroll();
if (null == mSlr) return;
if (mSlr.isFinished() && (!mPaused)) {
this.startScroll();
}
}
public int getRndDuration() {
return mRndDuration;
}
public void setRndDuration(int duration) {
this.mRndDuration = duration;
}
public boolean isPaused() {
return mPaused;
}
}
Ниже представлен класс Main Activity, реализующий ScrollTextView-
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ArabicActivity extends Activity {
public int woftxt;
public int flag = 0;
private TextView tv;
public ScrollTextView scrollRSSText;
private Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
private int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
int actual_scr_width = dm.widthPixels;
return actual_scr_width;
}
private String stringPadding(String str) {
flag = 0;
String strTxt = new String();
strTxt = str;
int fco = 0;
int swidth = getScreenWidth();
Rect rect = new Rect();
while (rect.width() < swidth) {
TextPaint tp = new TextPaint();
rect = new Rect();
tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
strTxt = strTxt + "$";
fco++;
}
if (fco > 1) {
flag = 1;
}
TextPaint tp = new TextPaint();
rect = new Rect();
tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
woftxt = rect.width();
strTxt = strTxt.substring(0, strTxt.indexOf("$"));
for (int i = 0; i < fco; i++) {
strTxt = strTxt + " ";
}
// strTxt=strTxt+"!!!!!!!!!!";
return strTxt;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arabic);
try{
// Step 3
// tt.setText(z.TickerText);
String Tdirection = "right";
String Tfont = "40";
String Tspeed = "50";
String Tfontsize = "40";
String Tfontcolor = "#FFFFFF";
String Tbackcolor = "#8B0000";
String Tstr = "مرحبا بكم في دبي-";
String speedset = null;
int tickerSpeed = Integer.parseInt(Tspeed);
if (tickerSpeed == 50) {
speedset = "fast";
} else if (tickerSpeed == 90) {
speedset = "medium";
} else if (tickerSpeed == 150) {
speedset = "slow";
}
ScrollTextView.screenwidth = getScreenWidth();
ScrollTextView scrollText = new ScrollTextView(this);
Tstr = stringPadding(Tstr);
scrollText.setGravity(Gravity.CENTER_VERTICAL);
scrollText.widthoftxt = woftxt;
scrollText.widthofscreen = getScreenWidth();
scrollText.setHeight((int)50);
if (Tdirection.equalsIgnoreCase("right")) {
scrollText.direc = "rtl";
} else {
scrollText.direc = "ltr";
}
scrollText.strln = Tstr.length();
scrollText.setX(0);
scrollText.setY(0);
scrollText.setTextSize(Float.parseFloat(Tfontsize));
scrollText.setText("ad"+"مرحبا بكم في دبي"); // if English string added than its working
//scrollText.setText("مرحبا بكم في دبي"); //No English string added so it is not working
scrollText.speed = speedset;
scrollText.setTextColor(Color.parseColor(Tfontcolor));
scrollText.setBackgroundColor(Color.parseColor(Tbackcolor));
scrollText.startScroll();
scrollText.setTextSize(Float.parseFloat(Tfontsize));
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
rl.addView(scrollText);
}
catch(Exception er){
Log.e("Errror",er.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.arabic, menu);
return true;
}
}