动画无法在SurfaceView中工作
问题描述:
我有这个非常简单的代码循环, 我没有做任何复杂的事情! 的TextAnimation()方法得到调用,但是,它里面的动画没有开始(我看不到任何日志)动画无法在SurfaceView中工作
这是我的鳕鱼:
我的主要活动:
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我的布局主:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineralayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<gamedevelopment.mahdi.nazari.killthemall_training.GameView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<LinearLayout
android:id="@+id/Li_ly"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<TextView
android:id="@+id/level_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:background="#fff"
android:gravity="center"
android:text="fdfdsf"
android:textColor="#000"
android:textSize="60dp" />
</LinearLayout>
我GameView:
public class GameView extends SurfaceView {
TextView level_text;
LinearLayout ly;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
start();
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
start();
}
public void start() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main, null);
level_text = (TextView) v.findViewById(R.id.level_text);
ly = (LinearLayout) v.findViewById(R.id.Li_ly);
TextAnimation();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
firstCreacation = false;
gameLoopThread.setRunning(false);
}
});
}
public void TextAnimation() {
Animation animation = AnimationUtils.loadAnimation(context, R.text_anim);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.e("start","");
}
@Override
public void onAnimationEnd(Animation animation) {
ShowLevelText2();
Log.e("End","");
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
level_text.startAnimation(animation);
}
}
任何人都可以帮助我吗?这段代码有什么问题? 谢谢:)
答
其实,你所做的一切都是错误的。 SurfaceView不应该与普通的View系统一起工作,至少不会低于Nougat。除此之外,你实际上并没有获得对已经充满膨胀的视图层次结构的引用,你正在给一个新的视图层次结构赋予一个新的视图层次结构,这个层次结构并没有显示在任何地方,而你正在尝试对它进行动画处理,它可能是动画效果,但是你看不到它,因为它是未在屏幕上显示。为了使该动画起作用,您需要将TextView的引用从Activity传递到SurfaceView,从构造函数或setter方法传递,然后为TextView引用设置动画效果。或者比这更好,从surfaceCreated回调到Activity并从Activity中播放动画。该代码可能是这个样子
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = findViewById(R.id.level_text);
GameView gameView = findViewById(R.id.game_view); //Give an id to your GameView, I'm just using a random id here
gameView.setTextView(textView);
}
}
现在里面GameView
public class GameView extends SurfaceView {
TextView textView;
... // All your code
public void setTextView(TextView t){
textView = t;
}
public void TextAnimation(){
... // All your code
textView.startAnimation(animation)
}
}
谢谢您的回答,是的,我发现我的问题yesterday.As你说,我膨胀并获得意见另一个参考,不屏幕上显示的是真实的。 – MehDi
@MehDi所以如果我是对的,那么将答案标记为接受,这样对其他人可能会有所帮助。 –
但是,如何通过充气视图获得相同的参考?我们夸大viw在代码上做些事情!有什么办法通过膨胀来获得相同的引用,而不是在Activity中初始化它并通过setter传递引用? – MehDi