Serviceを利用しカスタムビューをオーバーレイ表示しているのですが、描写はされるのですが、onTouchListenerの処理が実行されずに困っています。
内部クラスにカスタムビューのクラスを作り描写したら実行されるのかなと思いましたが、
The following classes could not be found:
-com.example.user.sample.MainActivity.DrawTest
とエラーが出てしまい、実行できませんでした。
java
1public class MainActivity extends Activity { 2 @Override 3 public void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 startService(new Intent(MainActivity.this, LayerService.class)); 6 } 7}
java
1public class DrawTest extends View implements View.OnTouchListener { 2 float x = 0, y = 0; 3 4 public DrawTest(Context context, AttributeSet atr) { 5 super(context, atr); 6 } 7 8 @Override 9 protected void onDraw(Canvas canvas) { 10 Paint paint = new Paint(); 11 paint.setColor(Color.argb(255, 255, 255, 255)); 12 13 paint.setAntiAlias(false); 14 canvas.drawLine(x, y, 500, 500, paint); 15 16 paint.setAntiAlias(true); 17 canvas.drawCircle(70, 70, 20.0f, paint); 18 19 paint.setStyle(Paint.Style.STROKE); 20 paint.setStrokeWidth(5); 21 canvas.drawCircle(100, 100, 10.0f, paint); 22 } 23 24 @Override 25 public boolean onTouch(View v, MotionEvent event) { 26 if (event.getAction() == MotionEvent.ACTION_DOWN) { 27 x = 400; 28 y = 400; 29 } 30 31 32 return true; 33 } 34 }
java
1public class LayerService extends Service { 2 View view; 3 WindowManager wm; 4 5 @Override 6 public void onStart(Intent intent, int startId) { 7 super.onStart(intent, startId); 8 9 // Viewからインフレータを作成する 10 LayoutInflater layoutInflater = LayoutInflater.from(this); 11 12 // 重ね合わせするViewの設定を行う 13 WindowManager.LayoutParams params = new WindowManager.LayoutParams( 14 WindowManager.LayoutParams.WRAP_CONTENT, 15 WindowManager.LayoutParams.WRAP_CONTENT, 16 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 17 WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 18 PixelFormat.TRANSLUCENT); 19 20 // WindowManagerを取得する 21 wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 22 23 // レイアウトファイルから重ね合わせするViewを作成する 24 view = layoutInflater.inflate(R.layout.activity_main,null); 25 26 // Viewを画面上に重ね合わせする 27 wm.addView(view, params); 28 } 29 30 @Override 31 public void onCreate() { 32 super.onCreate(); 33 } 34 35 @Override 36 public void onDestroy() { 37 super.onDestroy(); 38 39 // サービスが破棄されるときには重ね合わせしていたViewを削除する 40 wm.removeView(view); 41 } 42 43 @Override 44 public IBinder onBind(Intent intent) { 45 // TODO Auto-generated method stub 46 return null; 47 } 48}
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.user.sample.MainActivity" 7 android:id="@+id/layout"> 8 9 10 <com.example.user.sample.DrawTest 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:id="@+id/view" 14 android:layout_centerVertical="true" 15 android:layout_centerHorizontal="true" /> 16 17</RelativeLayout> 18
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。