我正在尝试制作一个程序,其中有3个图像视图,2个名为翻转和旋转的按钮以及一个方向键。我还没有弄清楚的是如何保存单击的imageview,所以目前只有一个特定的作品有效。
这是我的mainactivity.java:
package com.mdadi.animals;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
ImageView cat = findViewById(R.id.cat);
int tenDpAsPx = (int)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
getResources().getDisplayMetrics());
switch (v.getId()) {
case R.id.bird:
findViewById(R.id.bird_desc).setVisibility(View.VISIBLE);
//Cat & Dog TextViews set to invisible
findViewById(R.id.cat_desc).setVisibility(View.INVISIBLE);
findViewById(R.id.dog_desc).setVisibility(View.INVISIBLE);
break;
case R.id.cat:
findViewById(R.id.cat_desc).setVisibility(View.VISIBLE);
//Bird & Dog TextViews set to invisible
findViewById(R.id.bird_desc).setVisibility(View.INVISIBLE);
findViewById(R.id.dog_desc).setVisibility(View.INVISIBLE);
break;
case R.id.dog:
findViewById(R.id.dog_desc).setVisibility(View.VISIBLE);
//Bird & Cat TextViews set to invisible
findViewById(R.id.bird_desc).setVisibility(View.INVISIBLE);
findViewById(R.id.cat_desc).setVisibility(View.INVISIBLE);
break;
default:
break;
}
//Check which button is clicked and execute relevant code
switch (v.getId()) {
case R.id.centerBtn: //Reset ImgView translation to original position
cat.setTranslationX(0);
cat.setTranslationY(0);
break;
case R.id.upArrow: //Move ImgView up ^
cat.setTranslationY(cat.getTranslationY() - tenDpAsPx);
break;
case R.id.downArrow: //Move ImgView down v
cat.setTranslationY(cat.getTranslationY() + tenDpAsPx);
break;
case R.id.leftArrow: //Move ImgView to the left <-
cat.setTranslationX(cat.getTranslationX() - tenDpAsPx);
break;
case R.id.rightArrow: //Move ImgView to the right ->
cat.setTranslationX(cat.getTranslationX() + tenDpAsPx);
break;
case R.id.rotateBtn: //Rotate ImgView 90 degrees clockwise
cat.setRotation(cat.getRotation() + 90);
break;
case R.id.flipBtn: //Flip ImgView about its y axis (horizontally)
cat.setRotationY(cat.getRotationY() + 180);
break;
default:
break;
}
}
}
activity_main xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/rotateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:onClick="onClick"
android:text="@string/rotate_button" />
<Button
android:id="@+id/flipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@+id/rotateBtn"
android:onClick="onClick"
android:text="@string/flip_button" />
<ImageButton
android:id="@+id/centerBtn"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_above="@id/downArrow"
android:layout_toStartOf="@id/rightArrow"
android:background="@android:color/transparent"
android:onClick="onClick"
android:src="@drawable/ic_filter_center_focus_black_24dp" />
<ImageButton
android:id="@+id/leftArrow"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_toStartOf="@+id/centerBtn"
android:background="@android:color/transparent"
android:onClick="onClick"
android:src="@drawable/ic_arrow_back_black_24dp" />
<ImageButton
android:id="@+id/rightArrow"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:background="@android:color/transparent"
android:onClick="onClick"
android:src="@drawable/ic_arrow_forward_black_24dp" />
<ImageButton
android:id="@+id/upArrow"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_above="@+id/centerBtn"
android:layout_alignParentEnd="true"
android:layout_marginEnd="35dp"
android:background="@android:color/transparent"
android:onClick="onClick"
android:src="@drawable/ic_arrow_upward_black_24dp" />
<ImageButton
android:id="@+id/downArrow"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="@android:color/transparent"
android:onClick="onClick"
android:src="@drawable/ic_arrow_downward_black_24dp" />
<ImageView
android:id="@+id/bird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:onClick="onClick"
android:src="@drawable/bird" />
<ImageView
android:id="@+id/cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bird"
android:layout_marginBottom="100dp"
android:onClick="onClick"
android:src="@drawable/cat" />
<ImageView
android:id="@+id/dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cat"
android:onClick="onClick"
android:src="@drawable/dog" />
<TextView
android:id="@+id/bird_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/bird"
android:text="@string/bird"
android:visibility="invisible"/>
<TextView
android:id="@+id/cat_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/cat"
android:layout_below="@+id/bird"
android:text="@string/cat"
android:visibility="invisible"/>
<TextView
android:id="@+id/dog_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/dog"
android:layout_below="@+id/cat"
android:text="@string/dog"
android:visibility="invisible"/>
</RelativeLayout>
我该如何保存单击了哪个imgview?
答案 0 :(得分:0)
使您的布局如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp">
<ImageView
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myfunction"/>
</LinearLayout>
所以我想您只是想跟踪单击了哪个imageView
。因此,您可以做的是创建一个整数值,然后将图像的索引存储在那里。
例如,如果您有3张图片,则:
public void myfunction(View view){
switch(view.getParent()){
//tells you which linearlayout was clicked
}
}
您以后可以通过访问此变量来跟踪单击了哪个图像。
同样,如果您甚至想跟踪单击的图像,则可以使用ArrayList
中的Integer
,然后继续在其中添加值。
答案 1 :(得分:0)
如何创建名为int
的{{1}},并且每次单击图像时,都会为selectedImage
赋予一个值:
单击图像selectedImage
时。
然后在您的代码中检查selectedImage = 1
的值,并为获得足够的图像而做
答案 2 :(得分:0)
要检测按钮上的imageview,请在arraylist中添加选定的imageview,然后单击按钮,从arraylist中获取所有imageview id和标签,如下所示:
代码
在imageclick上添加视图
case R.id.img_2:
if (!imgviewlist.contains(img_2)) {
imgviewlist.add(img_2);
}
break;
点击按钮获取视图
case R.id.btn_1:
/*get selected images form array*/
for (int j = 0; j < imgviewlist.size(); j++) {
ImageView imageView = imgviewlist.get(j);
Log.e("get_imageview_id", "" + imageView.getId());
Log.e("get_imageview_tag", "" + imageView.getTag());
}
break;
完整代码
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
Button btn_1;
ImageView img_3, img_2, img_1;
ArrayList<ImageView> imgviewlist = new ArrayList<>();
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
/*intilize views*/
btn_1 = findViewById(R.id.btn_1);
img_3 = findViewById(R.id.img_3);
img_2 = findViewById(R.id.img_2);
img_1 = findViewById(R.id.img_1);
/*set on click listener*/
btn_1.setOnClickListener(this);
img_3.setOnClickListener(this);
img_2.setOnClickListener(this);
img_1.setOnClickListener(this);
/*set tag*/
img_1.setTag("img_1");
img_2.setTag("img_2");
img_3.setTag("img_3");
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_1:
/*get selected images form array*/
for (int j = 0; j < imgviewlist.size(); j++) {
ImageView imageView = imgviewlist.get(j);
Log.e("get_imageview_id", "" + imageView.getId());
Log.e("get_imageview_tag", "" + imageView.getTag());
}
break;
case R.id.img_1:
//check item already exist or not
if (!imgviewlist.contains(img_1)) {
imgviewlist.add(img_1); //add imageview in array
}
break;
case R.id.img_2:
if (!imgviewlist.contains(img_2)) {
imgviewlist.add(img_2);
}
break;
case R.id.img_3:
if (!imgviewlist.contains(img_3)) {
imgviewlist.add(img_3);
}
break;
}
}
}