Android活动真的很慢

时间:2015-06-12 12:47:45

标签: android performance android-activity

我用文字和图片写了一个活动。当用户点击时,两者都可以改变。它确实发生了变化,但确实很慢。我的点击和屏幕上的任何更改之间需要大约2秒钟,当在应用程序的其他部分时,此延迟可以忽略不计。

有人能告诉我我做错了吗?

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffc7c7c7"
android:gravity="center_horizontal"
android:id="@+id/root">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:id="@+id/image"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/lockscreen_neutral"
        android:padding="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Tuto"
        android:id="@+id/text"
        android:textSize="20sp"
        android:layout_weight="2"
        android:singleLine="false" />
</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tuto_prev"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentLeft="true"
    android:src="@drawable/arrow_prev"
    android:paddingLeft="10dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tuto_next"
    android:layout_alignParentRight="true"
    android:src="@drawable/arrow_next"
    android:layout_centerVertical="true" />

和Java:

package com.gmail.jan4984.fl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Nathanaël on 10/06/2015.
 */
@EActivity(R.layout.tutorial)
public class Tutorial extends Activity {
    public final String TAG = Tutorial.class.getSimpleName();

    @ViewById
    RelativeLayout Root;
    @ViewById
    ImageView Image;
    @ViewById
    TextView Text;
    @ViewById
    ImageView TutoPrev;
    @ViewById
    ImageView TutoNext;

    String[] tutoText;
    final int[] tutoImg = {R.drawable.lockscreen_neutral,  R.drawable.lockscreen_swipe_right, R.drawable.lockscreen_swipe_left, R.drawable.screen_home,R.drawable.screen_shop};
    int[] tutoTextBySlide;
    int slide,text;
    int screenWidth;
    //TranslateAnimation fadeOut, fadeIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
    }

    @AfterViews
    public void initSlide(){
        Image.setImageDrawable(getResources().getDrawable(tutoImg[0]));
        tutoText = getResources().getStringArray(R.array.tutorial);
        tutoTextBySlide = getResources().getIntArray(R.array.tuto_text_by_slide);
        Text.setText(tutoText[0]);
        TutoPrev.setVisibility(View.GONE);

    }


    @Click
    public void tuto_prev(){
        text--;
        setAnim();

    }
    @Click
    public void tuto_next(){
        text++;
        setAnim();

    }

    private void setAnim(){
        text = text < 0 ? 0 : text >= tutoTextBySlide[tutoTextBySlide.length-1]? text-1 : text;
        if(text == 0){
            TutoPrev.setVisibility(View.GONE);
            TutoNext.setVisibility(View.VISIBLE);
        }
        else  if ( text+1 >= tutoTextBySlide[tutoTextBySlide.length-1]){
            TutoPrev.setVisibility(View.VISIBLE);
            TutoNext.setVisibility(View.GONE);
        }
        else{
            TutoPrev.setVisibility(View.VISIBLE);
            TutoNext.setVisibility(View.VISIBLE);
        }

        //CustomAnim anim = new CustomAnim(0,-screenWidth/2, Text, false);
        Text.setText(tutoText[text]);
        if(text >= tutoTextBySlide[slide]){
            slide++;
            Image.setImageDrawable(getResources().getDrawable(tutoImg[slide]));
            //Image.startAnimation(anim);
        }

        Root.invalidate();
        //Text.startAnimation(anim);
    }
    /*
    private void endAnimation(View v, boolean fadeIn){
        if(v.equals(Text)){
            Text.setText(tutoText[text]);
        }
        else if(v.equals(Image))
            Image.setImageDrawable(getResources().getDrawable(tutoImg[slide]));
        if(!fadeIn)
            v.startAnimation(new CustomAnim(screenWidth/2,0,v,true));
        else
            v.clearAnimation();

    }*/

    @Click
    public void root(){
        if(text == tutoTextBySlide[tutoTextBySlide.length-1]) {
            startActivity(new Intent(this,HomeActivity_.class));
            finish();
        }
    }
    /*
    private class CustomAnim extends TranslateAnimation{

        boolean isFadeIn;
        View view;
        public CustomAnim(int start, int end, View v, final boolean fadeIn){
            super(Animation.RELATIVE_TO_SELF,start,Animation.RELATIVE_TO_SELF,end,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
            this.isFadeIn = fadeIn;
            view = v;

            super.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    endAnimation(view, isFadeIn);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {}
            });
            //super.setFillAfter(true);
            super.setDuration(200);
        }
/*
        @Override
        protected void applyTransformation(float time, Transformation t) {
            super.applyTransformation(time, t);
            //view.setAlpha(isFadeIn ? time : 1-time);
        }

    }*/
}

感谢您的回答。

0 个答案:

没有答案