こんにちは。
フレームレイアウトを拡張したカスタムビューで、カスタムビュー内のオブジェクトにObjectAnimatorを設定したいのですが、通常通りに設定すると、以下のエラーが出てしまいました。
どのようにObjectAnimatorを設定させればよいのでしょうか。
※申し訳ありません 今のコードに変更する前に投稿したコードが、一部のコード情報を消す際に不手際によりエラーコードとは異なる誤ったコードを投稿してしまいました。
このコードは別プロジェクトで再作成したものでありますのでこれが本物です。
お騒がせしてすみません。
↓MainActivity
java
1 2package developer.shion.com.tests; 3 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6 7public class MainActivity extends AppCompatActivity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 14 } 15}
activity_main
xml
1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <developer.shion.com.tests.CustomView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" /> 12</FrameLayout>
CustomView
java
1package developer.shion.com.tests; 2 3import android.animation.ObjectAnimator; 4import android.content.Context; 5import android.support.annotation.Nullable; 6import android.util.AttributeSet; 7import android.view.ViewGroup; 8import android.widget.FrameLayout; 9import android.widget.ImageView; 10import android.widget.LinearLayout; 11 12public class CustomView extends FrameLayout { 13 public CustomView(Context context, @Nullable AttributeSet attributeSet){ 14 super(context,attributeSet); 15 final ImageView imageView=new ImageView(context); 16 imageView.setImageResource(R.drawable.ic_launcher_background); 17 imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 18 addView(imageView); 19 20 new Thread(new Runnable() { 21 @Override 22 public void run() { 23 try { 24 Thread.sleep(1500); 25 } catch (InterruptedException e) { 26 e.printStackTrace(); 27 } 28 ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"translationX",0,500); 29 objectAnimator.setDuration(1000); 30 objectAnimator.start(); 31 } 32 }).start(); 33 } 34}
Log
108-10 10:30:02.854 19111-19158/developer.shion.com.tests E/AndroidRuntime: FATAL EXCEPTION: Thread-4 2 Process: developer.shion.com.tests, PID: 19111 3 android.util.AndroidRuntimeException: Animators may only be run on Looper threads 4 at android.animation.ValueAnimator.start(ValueAnimator.java:1011) 5 at android.animation.ValueAnimator.start(ValueAnimator.java:1065) 6 at android.animation.ObjectAnimator.start(ObjectAnimator.java:852) 7 at developer.shion.com.tests.CustomView$1.run(CustomView.java:30) 8 at java.lang.Thread.run(Thread.java:764)

回答1件
あなたの回答
tips
プレビュー