gpt4 book ai didi

java - Android 按钮的动画效果不符合预期

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:30 28 4
gpt4 key购买 nike

我正在尝试实现类似 this 的屏幕在安卓中。我从网站上获取了相同的代码并进行了尝试,但它没有为按钮设置动画。我得到this通过运行以下代码

我的content_main.xml包含以下代码:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="meetutu.juspay.com.meetutu.MainActivity"
tools:showIn="@layout/activity_main"
android:background="#2980B9">


<ImageView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/meetutuImageView"
android:src="@drawable/meetutuimage"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<RelativeLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/meetutuImageView"
android:layout_centerHorizontal="true">

<TextView
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="The joy of meeting an expert teacher"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="for a craving learner"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView1"/>

<Button
style="@style/Widget.AppCompat.Button.Colored"
android:id="@+id/loginButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="LOGIN"
android:layout_marginTop="44dp"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView1"
android:layout_alignStart="@+id/textView1" />

<Button
style="@style/Widget.AppCompat.Button.Colored"
android:id="@+id/signupButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="SIGN UP"
android:layout_alignTop="@+id/loginButton"
android:layout_alignRight="@+id/textView1"
android:layout_alignEnd="@+id/textView1" />



</RelativeLayout>


</RelativeLayout>

我的MainActivity.java包含以下代码:

package meetutu.juspay.com.meetutu;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.*;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private ImageView logoImage;
private TextView textView1, textView2;
private ViewGroup container;

public static final int STARTUP_DELAY = 300;
public static final int ANIM_ITEM_DURATION = 1000;
public static final int ITEM_DELAY = 300;

@Override
protected void onCreate(Bundle savedInstanceState) {

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);


}

@Override
public void onWindowFocusChanged(boolean hasFocus) {

if(hasFocus)
animation();
super.onWindowFocusChanged(hasFocus);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void animation()
{
logoImage = (ImageView) findViewById(R.id.meetutuImageView);
container = (ViewGroup) findViewById(R.id.container);

ViewCompat.animate(logoImage)
.translationY(-200)
.setStartDelay(STARTUP_DELAY)
.setDuration(ANIM_ITEM_DURATION)
.setInterpolator(
new DecelerateInterpolator(1.2f)).start();

for(int i = 0; i < container.getChildCount(); i++)
{
View v = container.getChildAt(i);
ViewPropertyAnimatorCompat viewAnimator;

if(!(v instanceof Button))
{
viewAnimator = ViewCompat.animate(v)
.translationY(50).alpha(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(1000);
}
else
{
viewAnimator = ViewCompat.animate(v)
.scaleY(1).scaleX(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(500);
}

viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
}
}
}

style.xml包含以下代码:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

我是 Android 新手,所以我不太了解动画的类型以及如何实现它们。我想实现链接中给出的动画和平滑度。请让我知道我还应该在代码中添加哪些内容才能使其像这样工作。

感谢您的宝贵时间!!

最佳答案

这是示例:-

Image

使用这些xml并将这4个放入drawable/anim文件夹

zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"
android:startOffset="500" >
</scale>

move_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromYDelta="10%p"
android:toYDelta="0%p"
android:duration="800"
android:startOffset="0" />
</set>

move_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromYDelta="-15%p"
android:toYDelta="0%p"
android:duration="500"
android:startOffset="300"/>
</set>

move_down_slow.xml

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromYDelta="-10%p"
android:toYDelta="0%p"
android:duration="300"
android:startOffset="500" />
</set>

将此代码用于 Oncreate() 方法中的动画..

将其声明为全局......

private Animation animation,animation1,animation2,animation3,animation4;

这是代码......

    animation3 = new AlphaAnimation(0.0f, 1.0f);
animation3.setFillAfter(true);
animation3.setDuration(1200);
mLinear.startAnimation(animation3);
mText.setText("Meetutu");
mText1.setText("The joy of meeting an expert teacher");
mText2.setText("for a craving learner");
animation = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_up);
mText.startAnimation(animation);
animation1 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down);
mText1.startAnimation(animation1);
animation2 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down_slow);
mText2.startAnimation(animation2);
animation4 = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
mButton.startAnimation(animation4);

注意:- mText、mText1、mText2 是 TextView , mButton 是 Button ,mLinear 是 layout.xml 文件中的父根......

享受编码......

关于java - Android 按钮的动画效果不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895369/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com