Mehmet Ethem SULAN :: Android :: Android when pressing touchscreen play sound

Android ile arkaplana atılan resme dokunduğumuz zaman bir dikdörtgen çiziyor

canvas.drawRect(left, top, left + 30, top + 50, paint);

ve MediaPlayer ile draw resourcenin altında bir telefon zili çalıyor

mediaPlayer=MediaPlayer.create(context, R.raw.phone_ring);



CustomDrawable.java

package com.custom.drawable;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

public class CustomDrawable extends Activity {

    private DrawView drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        drawView = new DrawView(this);
        drawView.setBackgroundResource(R.drawable.wallpaper);
        setContentView(drawView);
        drawView.requestFocus();
    }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==event.KEYCODE_BACK){
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	
}

DrawView.java

package com.custom.drawable;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public class DrawView extends View implements OnTouchListener {
	private Paint paint = new Paint();
	private float left;
	private float top;
	private boolean is_clicked = false;
	private MediaPlayer mediaPlayer;


	public DrawView(Context context) {
		super(context);
		setFocusable(true);
		setFocusableInTouchMode(true);

		this.setOnTouchListener(this);

		paint.setColor(Color.RED);
		paint.setAntiAlias(true);
		mediaPlayer=MediaPlayer.create(context, R.raw.phone_ring);
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (is_clicked) {
			canvas.drawRect(left, top, left + 30, top + 50, paint);
		}
	}

	public boolean onTouch(View view, MotionEvent event) {
		is_clicked = true;
//Tiklanan kordinatlar
		left = event.getX();
		top = event.getY();
		// Tasinmayi sagliyor
		invalidate();
		mediaPlayer.start();
		return true;
	}
	


}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/wallpaper"
    >    
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.custom.drawable"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CustomDrawable"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Download CustomDrawableSample.tar.gz

Write a Comment

Let me know what you think?