SoFunction
Updated on 2025-03-01

Android programming method to click on pictures to achieve switching effect

This article describes the method of Android programming to click on pictures to achieve switching effect. Share it for your reference, as follows:

Create a new Android project and name it FrameLayout

This instance mainly operates the class file in the src folder and the activity_main.xml layout file in res/layout

1. Layout the main page code activity_main.xml↓

<FrameLayout
xmlns:andro
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView 
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
android:src="@drawable/a"/>
<ImageView 
android:
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/b"/>
<ImageView 
android:
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/c"/>
</FrameLayout>

Code ↓

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
private String TAG = "FramLayoutTestActivity";
private ImageView image1;
private ImageView image2;
private ImageView image3;
private List<ImageView> list;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
image1 = (ImageView) findViewById(.image1);
image2 = (ImageView) findViewById(.image2);
image3 = (ImageView) findViewById(.image3);
list = new ArrayList<ImageView>();
(image1);
(image2);
(image3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (() == MotionEvent.ACTION_DOWN) {
(TAG, "move---");
showImage();
}
return (event);
}
private void showImage() {
//();
count = count % 3;
for (ImageView i : list) {
();
}
(count).setVisibility();
count++;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
}
}

3. Use the Android emulator or connect to an Android smartphone to run the project, click the program interface, and the pictures will automatically switch.

ps: The src link images in several ImageViews in the activity_main.xml file need to be copied from outside to the res/drawable directory in the project

I hope this article will be helpful to everyone's Android programming design.