درخواست راهنمایی برای حرکت متن در یک تکست ویو (حل شد)
مدیریت گفتگو
دوستان چطور میتونم یک متن رو در یک تکست ویو حرکت بدم بطوریکه مدام مثلا از چپ وارد و از راست خارج بشه؟
پاسخها 4
این میتونه کمکت کنه:
کلاس MoveActivity:
public class MoveActivity extends Activity implements AnimationListener {
private TextView movetxt;
private Button startBtn;
private Animation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.move_view);
// load the animation
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
// set animation listener
animation.setAnimationListener(this);
movetxt = (TextView) findViewById(R.id.moveText);
startBtn = (Button) findViewById(R.id.start);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// start the animation
movetxt.startAnimation(animation);
}
});
}
@Override
public void onAnimationEnd(Animation an) {
// when animation ends
if (an == animation) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationRepeat(Animation an) {
// if the animation repeats
if (an == animation) {
Toast.makeText(getApplicationContext(), "Animation Repeated",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationStart(Animation an) {
// when the animation is started
if (an == animation) {
Toast.makeText(getApplicationContext(), "Animation Started",
Toast.LENGTH_SHORT).show();
}
}
}
move_view.xml داخل پوشه layout در res:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/moveText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Javacodegeeks"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Start the animation" />
</LinearLayout>
move.xml داخل پوشه anim در res:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-75%p"
android:toXDelta="75%p"
android:duration="2000" >
</translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="800"
android:fromXDelta="75%p"
android:toXDelta="-75%p"
android:duration="2000" >
</translate>
</set>
منبع : javacodegeeks
نظرات
باید از marquee استفاده کنی همچنین برای متون فارسی:
این ویژگی رو به textview اضافه کن:
android:textDirection="rtl"
برای api زیر 17 این ویژگی رو اضافه کن :(ممکنه روی امولاتور درست کار نکنه ولی روی گوشی درست نمایش میده)
android:gravity="right"
مثال:
XML:<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="write something in Arabic,Persian,... here"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"/>
JAVA:TextView txt =(TextView) findViewById(R.id.txt); txt.setSelected(true);
نظرات
فکر کنم خیلی راحتر هم بشه این کارو کرد
یه انیمیشن بساز و روی تکست ویو اعمال کن انیمیش از یه طرف بیاد و از یه طرف بره بیرون ! تکرار هم تا بینهایت !
موفق باشید .
نظرات
با تشکر از دوستان بخاطر پاسخگویی بالاخره مشکل حل شد، فقط یکم توضیحات کاملتری میدم. چون میخوایم تکست کامل از صفحه خارج بشه پس بجای 75 (در move.xml @Ali) از 100 و 100- استفاده کردم و برای اینکه پشت سرهم این عمل انجام بشه از thread استفاده کردم.
نکته دیگر اینکه در ابتدا باید تکست ویو invisible باشه و باید دائماٌ بین visible و invisible تکست ویو toggle کنیم تا انیمیشن به شکل حرکت از یک سمت به سمت دیگر به نظر برسه
تابع move.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="-100%p"
android:fillAfter="true"
android:duration="2000"/>
</set>
حلقه تکرار : (duration مقداری است که برای سرعت انیمیشن درنظر گرفتیم و showText در ابتدا true است)
new Thread(new Runnable() {
@Override
public void run() {
while (showText) {
try {
Thread.sleep(duraiton);
G.HANDLER.post(new Runnable() {
@Override
public void run() {
txtPrimiry.setVisibility(View.VISIBLE);
txtPrimiry.startAnimation(move);
txtPrimiry.setVisibility(View.INVISIBLE);
move.setDuration(duraiton);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
خوب داکیومنت این انیمیشن رو خوندم مثل اینکه برای تکرار احتیاجی به حلقه بالا نیست و با کد زیر حله:
Animation move = AnimationUtils.loadAnimation(this, R.anim.move);
move.setRepeatCount(-1);
در حقیقت میتونیم تعیین کنیم چقدر این انیمیشن تکرار بشه و "1- " به معنای تکرار دائم هستش.
فقط الان یه مشکل جدید با کم و زیاد کردن سرعت انیمیشن بصورت داینامیک دارم که اون رو بصورت یه سوال جداگانه در پرسش و پاسخ مطرح میکنم.
نظرات
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
نظرات