我制作了一个Android应用程序,我想在每个图像上应用放大和缩小。我的代码是全屏显示图像,并且当我点击图像放大和缩小效果时,我想要下一个和后面的两个按钮。 我想要在下一次和后面点击时更改图像,并且还希望每张图像都有缩放效果。我可以这样做。请帮助我先生。
public class MainActivity extends Activity {
Button btn1, btn2;
ImageView ib;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (ImageView)findViewById(R.id.imageView1);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
}
public OnClickListener onClickListener = new OnClickListener()
{
int counter = 0;
// The images are located in res/drawable-xxhdpi
final Integer pics[] =
{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
R.drawable.f,
R.drawable.g,
R.drawable.h,
R.drawable.i,
R.drawable.j,
R.drawable.k,
R.drawable.l,
R.drawable.m,
R.drawable.n,
R.drawable.o,
R.drawable.p,
R.drawable.q,
R.drawable.r,
R.drawable.s,
R.drawable.t,
R.drawable.u,
R.drawable.v,
R.drawable.w,
R.drawable.x,
};
@Override
public void onClick(final View v)
{
switch(v.getId())
{
case R.id.button1:
if(counter < 25)
{
counter++;
ib.setImageResource(pics[counter]);
}
break;
case R.id.button2:
if(counter > 0)
{
counter--;
ib.setImageResource(pics[counter]);
}
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<TableLayout 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"
tools:context=".MainActivity" >
<TableRow
android:id="@+id/tb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next"
android:layout_weight="1"
android:onClick="onClick" />
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/back"
android:layout_weight="1"
android:onClick="onClick"/>
</TableRow>
<ImageButton
android:id="@+id/img1"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/alhambra" />
</TableLayout>