View Full Version : درخواست کد انیمیشن
dalmif
شنبه 09 اسفند 1393, 15:04 عصر
سلام
ببخشید.
نمی دونم دیدید یا نه، در بازار وقتی میری به برنامه ای نظر بدی میتونی نظرات رو هم بخونی و وقتی روی نظرات کلیک می کنی برعکس میشه و عکس لایک و دیس لایک رو نشون میده که میشه انتخاب کرد.
من می خواستم بدونم چجوری ظاهر رو تغییر میده و اون انیمیشنی رو که برعکس میکنه رو هم می خواستم.
ممنون میشم هرکی جوابم رو بده
با تشکر
atpf67
شنبه 09 اسفند 1393, 15:48 عصر
سلام
کلاس انیمیشن:
FlipAnimation.java
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpo lator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class FlipAnimation extends Animation {
private Camera camera;
private View fromView;
private View toView;
private float centerX;
private float centerY;
private boolean forward = true;
/**
* Creates a 3D flip animation between two views.
*
* @param fromView
* First view in the transition.
* @param toView
* Second view in the transition.
*/
public FlipAnimation(View fromView, View toView) {
this.fromView = fromView;
this.toView = toView;
setDuration(400);
setFillAfter(false);
setInterpolator(new AccelerateDecelerateInterpolator());
}
public void reverse() {
forward = false;
View switchView = toView;
toView = fromView;
fromView = switchView;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
centerX = width / 2;
centerY = height / 2;
camera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// Angle around the y-axis of the rotation at the given time
// calculated both in radians and degrees.
final double radians = Math.PI * interpolatedTime;
float degrees = (float) (180.0 * radians / Math.PI);
// Once we reach the midpoint in the animation, we need to hide the
// source view and show the destination view. We also need to change
// the angle by 180 degrees so that the destination does not come in
// flipped around
if (interpolatedTime >= 0.5f) {
degrees -= 180.f;
fromView.setVisibility(View.GONE);
toView.setVisibility(View.VISIBLE);
}
if (forward)
degrees = -degrees; // determines direction of rotation when flip
// begins
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateX(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
لیوت اکتیویتی :
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/face"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#a7c7b7"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Face of View"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#d7e7F7"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back of View"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
فایل MainActivity:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.root).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flipCard(findViewById(R.id.root), findViewById(R.id.face), findViewById(R.id.back), 500);
}
});
}
private void flipCard(View rootLayout, View cardFace, View cardBack, long duration) {
FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack);
flipAnimation.setDuration(duration);
if (cardFace.getVisibility() == View.GONE) {
flipAnimation.reverse();
}
rootLayout.startAnimation(flipAnimation);
}
}
dalmif
شنبه 09 اسفند 1393, 16:00 عصر
خدا خیرت بده
ممنون
AliRezaBeytari
شنبه 09 اسفند 1393, 16:26 عصر
خیلی از سورسی که قرار دادید ممنونم جناب atpf67 !!
فقط اگر میشه به صورت وارون اون رو هم قرار بدید ؛ یا بگید باید کدوم قسمت از کد رو تغییر بدیم ؟؟!!
خیلی ممنونم.
موفق باشید.
atpf67
شنبه 09 اسفند 1393, 16:34 عصر
خیلی از سورسی که قرار دادید ممنونم جناب atpf67 !!
فقط اگر میشه به صورت وارون اون رو هم قرار بدید ؛ یا بگید باید کدوم قسمت از کد رو تغییر بدیم ؟؟!!
خیلی ممنونم.
موفق باشید.
خواهش میکنم استاد
راستش منظورتون رو از وارون متوجه نشدم اگر ممکنه بیشتر توضیح بدید یا شکلی رسم کنید که متوجه بشم.
AliRezaBeytari
شنبه 09 اسفند 1393, 16:48 عصر
خواهش میکنم استاد
راستش منظورتون رو از وارون متوجه نشدم اگر ممکنه بیشتر توضیح بدید یا شکلی رسم کنید که متوجه بشم.
بله درسته !! من منظورم رو درست بیان نکردم.
منظورم این بود که در جهت دیگه انیمیشن اعمال بشه !! جهت افقی !!
atpf67
شنبه 09 اسفند 1393, 17:14 عصر
بله درسته !! من منظورم رو درست بیان نکردم.
منظورم این بود که در جهت دیگه انیمیشن اعمال بشه !! جهت افقی !!
برای این کار کافیه این کد را:
camera.rotateX(degrees);
به این صورت تغییر بدید:
camera.rotateY(degrees);
و البته در حالت افقی اگر یک خط قبل از اون یعنی بعد از camera.save خط کد زیر را اضافه کنید نتیجه مطلوب تر خواهد بود.
camera.translate(0, 0, Math.abs(degrees)*2);
AliRezaBeytari
شنبه 09 اسفند 1393, 17:26 عصر
برای این کار کافیه این کد را:
camera.rotateX(degrees);
به این صورت تغییر بدید:
camera.rotateY(degrees);
و البته در حالت افقی اگر یک خط قبل از اون یعنی بعد از camera.save خط کد زیر را اضافه کنید نتیجه مطلوب تر خواهد بود.
camera.translate(0, 0, Math.abs(degrees)*2);
خیلی ممنونم.
این خط کد که گفتید :
camera.translate(0, 0, Math.abs(degrees)*2);
زیبا هست ، اما موقع تمام شدن animation یک دفعه بر میگرده به حالت قبل. نمیشه یه جور نرم به حالت اول بره ؟؟!!!
خیلی ممنونم.
atpf67
شنبه 09 اسفند 1393, 17:50 عصر
خیلی ممنونم.
این خط کد که گفتید :
camera.translate(0, 0, Math.abs(degrees)*2);
زیبا هست ، اما موقع تمام شدن animation یک دفعه بر میگرده به حالت قبل. نمیشه یه جور نرم به حالت اول بره ؟؟!!!
خیلی ممنونم.
متأسفانه راهی براش پیدا نکردم. اگر شما موفق شدید حتماً اینجا بذارید.
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.