我有一个Android应用程序。其中,滑块工作正常,分享按钮也正常工作。获取当前图像以在Android中共享它
但是,共享按钮只共享一个图像,我在代码中设置了滑块中的三个总图像(因为我用这种方式编码),而不是在滑块库中可见的当前图像。我只是想调用当前图像的共享按钮代码,以便用户可以共享当前图像。
以下是一些细节, 3 ImageView 3滑块中的图像。另外,我已经在ViewFlipper采取的这些:
图像1
android:id="@+id/imageview1"
android:tag="bg1"
android:src="@drawable/image1
图像2
android:id="@+id/imageview2"
android:tag="bg2"
android:src="@drawable/image2
图像3
android:id="@+id/imageview3"
android:tag="bg3"
android:src="@drawable/image3
到目前为止,我已试过,
Button btShare = (Button) findViewById(R.id.shareImage);
btShare.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
share();
}
});
现在,这里是share()函数代码片断,其中我想要当前图像。为此,我采取了setTag()和getTag(),但我仍然感到困惑。 在这里,我为所有3张图片设置了标签,以便我可以获得它们的价值。
mImageView=(ImageView)findViewById(R.id.imageview1);
mImageView.setTag("bg1");
mImageView=(ImageView)findViewById(R.id.imageview2);
mImageView.setTag("bg2");
mImageView=(ImageView)findViewById(R.id.imageview3);
mImageView.setTag("bg3");
String s = String.valueOf(mViewFlipper.getTag());
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
if (s.equals("bg1"))/
{
// now the code for share button starts, in that a folder gets
created in the Directory and a default name is given to the
image which gets saved there, like
File f = new File(dir, "savedImage1.png");
我不知道(我不想做,要么)有关上述的if-else过,因为这是不好的创建“如果”的每张图片,所以我需要一个备用像一个数组)左右,以解决当前可绘制图像。
所以在上面的字符串s是返回“null”,在选择任何图像(我通过应用Toast检查它,它在每种情况下显示为空)。
如果我成功获取bg1(对于图像1),或bg2(对于图像2)或bg3(对于图像3),对于当前图像在字符串s中,则可以轻松地运行代码以共享一个只有特定图像出现在滑块库上时。
谢谢你,@Axarydax。我了解了flipper.getDisplayedChild();它的工作完美。 –
但是,我仍然对如何创建一个数组并获取它的值感到困惑,因为我必须调用一次BITMAP。意思是我不想像下面那样分开来做,bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image1);对于image1等 –
@AmanChawla你可以在你的活动中定义一个int []图像ID数组,并在其中包含R.drawable.image1,R.drawable.image2等值。如果您保持与ViewFlipper相同的顺序,则可以使用indexOfChild作为数组的索引来获取图像ID。 – Axarydax