[英]How to add zoom feature without loosing the x and y layout coordinates
我有一張圖片,顯示在不同部分觸摸時的不同吐司。 這是代碼。
public class MainActivity extends Activity implements OnTouchListener
{
String xcoordinate, ycoordinate;
Integer xint, yint;
Double xdoub, ydoub;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View touchView = findViewById(R.id.lyt);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
xcoordinate = String.valueOf(event.getX());
ycoordinate = String.valueOf(event.getY());
xdoub = Double.valueOf(xcoordinate);
ydoub = Double.valueOf(ycoordinate);
xint = xdoub.intValue();
yint = ydoub.intValue();
Toast.makeText(getApplicationContext(), "Touch coordinates : " +
String.valueOf(event.getX()) + "x and " + String.valueOf(event.getY()) + "y", Toast.LENGTH_SHORT).show();
LaunchPopup();
return true;
}
});
}
public void LaunchPopup()
{
if(xint > 243 && xint < 307 && yint > 315 && yint < 410)
{
Toast.makeText(getApplicationContext(), "Center", Toast.LENGTH_SHORT).show();
}
else if(xint > 330 && xint < 394 && yint > 24 && yint < 122)
{
Toast.makeText(getApplicationContext(), "Manual 68 Section", Toast.LENGTH_SHORT).show();
}
else if(xint > 330 && xint < 394 && yint > 154 && yint < 250)
{
Toast.makeText(getApplicationContext(), "Manual 79 Section", Toast.LENGTH_SHORT).show();
}
else if(xint > 330 && xint < 394 && yint > 314 && yint < 410)
{
Toast.makeText(getApplicationContext(), "Manual 17 Section", Toast.LENGTH_SHORT).show();
}
else if(xint > 330 && xint < 394 && yint > 458 && yint < 554)
{
Toast.makeText(getApplicationContext(), "Manual 45 Section", Toast.LENGTH_SHORT).show();
}
else if(xint > 330 && xint < 394 && yint > 602 && yint < 700)
{
Toast.makeText(getApplicationContext(), "Group Task Section", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
現在,當圖像適合屏幕顯示時,它可以正常工作。 但是我想要的是在圖像上添加縮放功能,而又不會在圖像的特定部分上失去特定烤面包的主要功能。 有沒有辦法做到這一點? 我的意思是,有什么可以代表x和y坐標變化的縮放比例。 因此,即使在縮放后,在該特定區域中觸摸圖像時,圖像仍應執行相同的操作。
更新我剛剛給TouchImageView進行了新的更新。 現在,除了“平移”和“縮放”縮放外,還包括“雙擊縮放”和“縮放”。 下面的代碼非常過時。 您可以簽出github項目以獲取最新代碼 。
用法將TouchImageView.java放在您的項目中。 然后可以與ImageView一樣使用。 例:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
如果在xml中使用TouchImageView,則必須提供完整的程序包名稱,因為它是自定義視圖。 例:
<com.example.touch.TouchImageView
android:id="@+id/img”
android:layout_width="match_parent"
android:layout_height="match_parent" />
注意:我已經刪除了先前的答案,其中包括一些非常舊的代碼,現在直接鏈接到github上的最新代碼。
ZoomableImageView img = (ZoomableImageView) findViewById(R.id.img);
如果您在xml中使用ZoomableImageView,則必須提供完整的程序包名稱,因為它是一個自定義視圖。 例:
<com.example.touch.ZoomableImageView
android:id="@+id/img”
android:layout_width="match_parent"
android:layout_height="match_parent" />
除此之外,您可以繼續瀏覽項目https://github.com/laurencedawson/ZoomableImageView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.