我正在构建我的第一个android项目/第一个java项目。我正在尝试绘制一个图像,其顶部有多个按钮,用户可以单击它们。然而,在放大按钮后,按照我的希望放大并保持其与背景中图像的相对位置,但它不再响应onClickListener。
XML代码
<FrameLayout
android:id="@+id/DeckLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_horizontal" >
<!-- <CustomDrawableView android:layout_height="fill_parent"
android:layout_width="fill_parent"
/> -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="420dp"
android:clickable="false"
android:scaleType="fitCenter"
android:src="@drawable/deck12" />
<Button
android:id="@+id/QM2_12002"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="@+id/zoom_in"
android:layout_width="102dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/zoom_in" />
</FrameLayout>
</LinearLayout>
Java代码
package com.sgs.hotelguru;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MapsExample extends Activity {
View mainView = null;
final float Zoom_max = 5f;
private static final String TAG = "Die Karte";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_example);
mainView =findViewById(R.id.DeckLayout);
//Button buttonZoomOut = (Button)findViewById(R.id.zoom_out);
//Button buttonNormal = (Button)findViewById(R.id.reset);
Button buttonZoomIn = (Button)findViewById(R.id.zoom_in);
/*buttonZoomOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(0.5f,0.5f,new PointF(0,0));
}
});
buttonNormal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(1f,1f,new PointF(0,0));
}
});*/
buttonZoomIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(new PointF(0,0));
}
});
mainView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
@Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
Button QM2_12002 = (Button)findViewById(R.id.QM2_12002);
QM2_12002.setX(300);
QM2_12002.setY(400);
QM2_12002.setEnabled(true);
QM2_12002.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MapsExample.this,
"YOU CLICKY THE BUTTON YAY!",
Toast.LENGTH_SHORT).show();
}
});
}
});
}
/** zooming is done from here */
public void zoom(PointF pivot){
mainView.setPivotX(pivot.x);
mainView.setPivotY(pivot.y);
float workScaleX = mainView.getScaleX();
float workScaleY = mainView.getScaleY();
if(workScaleX<Zoom_max)
{
mainView.setScaleX(workScaleX+1f);
mainView.setScaleY(workScaleY+1f);
}
}
}
答案 0 :(得分:0)
// try this
public class MainActivity extends Activity {
View mainView;
Button QM2_12002;
Button buttonZoomIn;
final float Zoom_max = 5f;
private static final String TAG = "Die Karte";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_example);
mainView =findViewById(R.id.DeckLayout);
//Button buttonZoomOut = (Button)findViewById(R.id.zoom_out);
//Button buttonNormal = (Button)findViewById(R.id.reset);
buttonZoomIn = (Button)findViewById(R.id.zoom_in);
QM2_12002 = (Button)findViewById(R.id.QM2_12002);
/*buttonZoomOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(0.5f,0.5f,new PointF(0,0));
}
});
buttonNormal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(1f,1f,new PointF(0,0));
}
});*/
buttonZoomIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zoom(new PointF(0,0));
}
});
QM2_12002.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"YOU CLICKY THE BUTTON YAY!",
Toast.LENGTH_SHORT).show();
}
});
mainView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
@Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
QM2_12002.setX(300);
QM2_12002.setY(400);
QM2_12002.setEnabled(true);
}
});
}
/** zooming is done from here */
public void zoom(PointF pivot){
mainView.setPivotX(pivot.x);
mainView.setPivotY(pivot.y);
float workScaleX = mainView.getScaleX();
float workScaleY = mainView.getScaleY();
if(workScaleX<Zoom_max)
{
mainView.setScaleX(workScaleX+1f);
mainView.setScaleY(workScaleY+1f);
}
}
}