barshahi 1 ارسال شده در ۱۸ اردیبهشت ۱۳۹۴ گزارش اشتراک گذاری ارسال شده در ۱۸ اردیبهشت ۱۳۹۴ با سلام من توی برنامم ی کلاس از imageView ارث بری کردم و از اون کلاس برای نمایش تصویر روی اکتیویتی استفاده کردم! مشکلی که هست اینه که رویداد LongClick کار نمیکنه!!! کدهای زیر مربوط به برناممه! اینا کد xml: [shcode=xml] <?xml version="1.0" encoding="utf-8"?> xmlns:ctc="http://schemas.android.com/apk/res/com.ctc.android.widget" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:longClickable="true" android:src="@drawable/background" ctc:map="usamap" /> [/shcode] اینا کد اکتیویتیمه که رویداد کلیکو اینجا فراخوانی میکنم: [shcode=java] public class ImageMapTestActivity extends Activity implements SensorListener { private static final int FORCE_THRESHOLD = 350; private static final int TIME_THRESHOLD = 100; private static final int SHAKE_TIMEOUT = 500; private static final int SHAKE_DURATION = 1000; private static final int SHAKE_COUNT = 3; private long then; private int longClickDuration = 1000; private SensorManager mSensorMgr; private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f; private long mLastTime; private Context mContext; private int mShakeCount = 0; private long mLastShake; private long mLastForce; MediaPlayer mp; ImageMap mImageMap; Button btn_menu, btn_ritms; boolean play; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_menu = (Button) findViewById(R.id.btn_menu); btn_ritms = (Button) findViewById(R.id.btn_ritmha); play = false; initPlay(1); btn_ritms.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (play) { mp.stop(); btn_ritms .setBackgroundResource(R.drawable.ic_action_overflow); play = false; } else { openNewGameDialog(); btn_ritms.setBackgroundResource(R.drawable.ic_stop); } } }); btn_menu.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub btn_ritms.setBackgroundResource(R.drawable.ic_action_overflow); play = false; Intent next = new Intent(getBaseContext(), ContentActivity.class); startActivity(next); } }); // find the image map in the view mImageMap = (ImageMap) findViewById(R.id.map); // add a click handler to react when areas are tapped mImageMap.setLongClickable(true); mImageMap.setClickable(true); mImageMap .addOnImageMapClickedHandler(new ImageMap.OnImageMapClickedHandler() { @Override public void onImageMapClicked(int id) { // when the area is tapped, show the name in a // text bubble Log.d("ImageMapTestActivity", "onImageMapClicked"); mImageMap.showBubble(id); } @Override public void onBubbleClicked(int id) { // react to info bubble for area being tapped } }); mImageMap.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View arg0) { // TODO Auto-generated method stub Log.d("Long Click :", "*******************"); Toast.makeText(ImageMapTestActivity.this, "long Click", 0) .show(); return true; } }); } private void initPlay(int j) { Log.d("ritm load is : ", "ritm" + j); mp = new MediaPlayer(); try { mp.prepare(); mp.stop(); mp.release(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (j == 0) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm1); Log.d("ritm load is : ", "ritm" + j); } else if (j == 1) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm2); Log.d("ritm load is : ", "ritm" + j); } else if (j == 2) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm3); Log.d("ritm load is : ", "ritm" + j); } else if (j == 3) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm4); Log.d("ritm load is : ", "ritm" + j); } else if (j == 4) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm5); Log.d("ritm load is : ", "ritm" + j); } else if (j == 5) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm6); Log.d("ritm load is : ", "ritm" + j); } else if (j == 6) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm7); Log.d("ritm load is : ", "ritm" + j); } else if (j == 7) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm8); Log.d("ritm load is : ", "ritm" + j); } else if (j == 8) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm9); Log.d("ritm load is : ", "ritm" + j); } else if (j == 9) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm10); Log.d("ritm load is : ", "ritm" + j); } else if (j == 10) { mp = MediaPlayer.create(getBaseContext(), R.raw.ritm11); Log.d("ritm load is : ", "ritm" + j); } } private void openNewGameDialog() { new AlertDialog.Builder(this).setTitle(R.string.app_name) .setItems(R.array.ritms, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i) { play = false; playRitm(i); btn_ritms.setBackgroundResource(R.drawable.ic_stop); } }).show(); } private void playRitm(int i) { initPlay(i); // sp.play(ritm, 1.0f, 1.0f, 0, -1, 1.0f); mp.setLooping(true); mp.start(); /* * if (i == 0) { sp.play(ritm1, 1.0f, 1.0f, 1, -1, 1.0f); * sp.stop(ritm2); sp.stop(ritm3); sp.stop(ritm4); sp.stop(ritm5); * sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm8); sp.stop(ritm9); * sp.stop(ritm10); * * } else if (i == 1) { sp.stop(ritm1); sp.stop(ritm3); sp.stop(ritm4); * sp.stop(ritm5); sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm8); * sp.stop(ritm9); sp.stop(ritm10); sp.play(ritm2, 1.0f, 1.0f, 1, -1, * 1.0f); * * } else if (i == 2) { sp.stop(ritm1); sp.stop(ritm2); sp.stop(ritm4); * sp.stop(ritm5); sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm8); * sp.stop(ritm9); sp.stop(ritm10); sp.play(ritm3, 1.0f, 1.0f, 1, -1, * 1.0f); * * } else if (i == 3) { sp.stop(ritm1); sp.stop(ritm2); sp.stop(ritm3); * sp.stop(ritm5); sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm8); * sp.stop(ritm9); sp.stop(ritm10); sp.play(ritm4, 1.0f, 1.0f, 1, -1, * 1.0f); * * } else if (i == 4) { sp.stop(ritm1); sp.stop(ritm2); sp.stop(ritm3); * sp.stop(ritm4); sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm8); * sp.stop(ritm9); sp.stop(ritm10); sp.play(ritm5, 1.0f, 1.0f, 1, -1, * 1.0f); } else if (i == 5) { sp.stop(ritm1); sp.stop(ritm2); * sp.stop(ritm3); sp.stop(ritm4); sp.stop(ritm5); sp.stop(ritm7); * sp.stop(ritm8); sp.stop(ritm9); sp.stop(ritm10); sp.play(ritm6, 1.0f, * 1.0f, 1, -1, 1.0f); } else if (i == 6) { sp.stop(ritm1); * sp.stop(ritm2); sp.stop(ritm3); sp.stop(ritm4); sp.stop(ritm5); * sp.stop(ritm6); sp.stop(ritm8); sp.stop(ritm9); sp.stop(ritm10); * sp.play(ritm7, 1.0f, 1.0f, 1, -1, 1.0f); } else if (i == 7) { * sp.stop(ritm1); sp.stop(ritm2); sp.stop(ritm3); sp.stop(ritm4); * sp.stop(ritm5); sp.stop(ritm6); sp.stop(ritm7); sp.stop(ritm9); * sp.stop(ritm10); sp.play(ritm8, 1.0f, 1.0f, 1, -1, 1.0f); } else if * (i == 8) { sp.stop(ritm1); sp.stop(ritm2); sp.stop(ritm3); * sp.stop(ritm4); sp.stop(ritm5); sp.stop(ritm6); sp.stop(ritm7); * sp.stop(ritm8); sp.stop(ritm10); sp.play(ritm9, 1.0f, 1.0f, 1, -1, * 1.0f); } else if (i == 9) { sp.stop(ritm1); sp.stop(ritm2); * sp.stop(ritm3); sp.stop(ritm4); sp.stop(ritm5); sp.stop(ritm6); * sp.stop(ritm7); sp.stop(ritm8); sp.stop(ritm9); sp.play(ritm10, 1.0f, * 1.0f, 1, -1, 1.0f); } */ play = true; } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mp.stop(); if (mSensorMgr != null) { mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER); mSensorMgr = null; } } public void resume() { mp.stop(); mSensorMgr = (SensorManager) mContext .getSystemService(Context.SENSOR_SERVICE); if (mSensorMgr == null) { throw new UnsupportedOperationException("Sensors not supported"); } boolean supported = mSensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME); if (!supported) { mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER); throw new UnsupportedOperationException( "Accelerometer not supported"); } } @Override public void onAccuracyChanged(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onSensorChanged(int sensor, float[] values) { // TODO Auto-generated method stub if (sensor != SensorManager.SENSOR_ACCELEROMETER) return; long now = System.currentTimeMillis(); if ((now - mLastForce) > SHAKE_TIMEOUT) { mShakeCount = 0; Toast.makeText(getApplicationContext(), "1111111", 0); } if ((now - mLastTime) > TIME_THRESHOLD) { long diff = now - mLastTime; float speed = Math.abs(values[sensorManager.DATA_X] + values[sensorManager.DATA_Y] + values[sensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000; if (speed > FORCE_THRESHOLD) { Toast.makeText(getApplicationContext(), "2222222", 0); if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) { mLastShake = now; mShakeCount = 0; Toast.makeText(getApplicationContext(), "3333333", 0); } mLastForce = now; Toast.makeText(getApplicationContext(), "444444444", 0); } mLastTime = now; mLastX = values[sensorManager.DATA_X]; mLastY = values[sensorManager.DATA_Y]; mLastZ = values[sensorManager.DATA_Z]; } } }[/shcode] نقل قول لینک ارسال به اشتراک گذاری در سایت های دیگر
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .