如何获取可绘制图像的ID

时间:2022-02-22 06:40:56

I'm relatively new to android and I'm working on a horizontal scroll view with drag and drop functionality. Right now everything is working perfectly fine I can drag the image and drop it in the drop zone with count of failed and successful drops. For now the image that is being dragged and its shadow looks the same. What I want is that when I drag an image the shadow that appears for that image is an image from drawables that I select.

我对android比较陌生,我正在使用拖放功能在水平滚动视图上工作。现在一切都运行得很好,我可以拖拽图像,并将它拖到下拉区,计数失败和成功的下拉。现在,被拖动的图像和它的影子看起来是一样的。我想要的是,当我拖动一个图像时,那个图像所显示的阴影就是我选择的可绘制的图像。

Here is the code for shadow builder:

下面是影子构建器的代码:

 void dragAndDropImage(int imageId)
{
   // int drawableid = getResources().getIdentifier("drawable/a1", "id", getPackageName());
    final ImageView drag = (ImageView)findViewById(imageId);

    drag.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            // TODO Auto-generated method stub
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);//I want the drawable id here View.DragShadowBuilder(drawableid)
            v.startDrag(data, shadow, null, 0);
            return false;
        }
    });

I want to get the id of one particular image from drawables and then I can use it in View.DragShadowBuilder(id of the image from drawables) to change the image of the shadow. Any help in this regard is appreciated.

我想从drawables获得一个特定图像的id,然后我可以在View中使用它。DragShadowBuilder(来自drawables的图像的id)来改变阴影的图像。在这方面的任何帮助都是值得感激的。

5 个解决方案

#1


1  

It will be something like:

会是这样的:

R.drawable.resourcename

Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).

确保你没有Android。导入的R名称空间可能会混淆Eclipse(如果您使用的是这个名称空间)。

If that doesn't work, you can always use a context's getResources method ...

如果这不起作用,您可以始终使用上下文的getResources方法……

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);

Where this.context is intialised as an Activity, Service or any other Context subclass.

这个地方。上下文被作为活动、服务或任何其他上下文子类进行初始化。

Update:

更新:

If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?

如果它是您想要的名称,那么Resources类(由getResources()返回)具有getResourceName(int)方法,以及getResourceTypeName(int)?

Update 2:

更新2:

The Resources class has this method:

资源类有这个方法:

public int getIdentifier (String name, String defType, String defPackage) Which returns the integer of the specified resource name, type & package.

公共int getIdentifier (String name, String defType, String defPackage)返回指定资源名、类型和包的整数。

#2


0  

String uri = "@drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());

Can you try this?

你可以试试这个吗?

#3


0  

Try this,

试试这个,

final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);

#4


0  

Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.

假设您的Android资源文件夹中有几个可绘制文件,以及需要迭代它们的内容。您使用一个特定的命名模式,并希望使用它来确定资源ID。

The following steps shows how to get a resource ID based on the resource name:

下面的步骤展示了如何根据资源名获取资源ID:

   // Name of resource 
  //Type
 // Package of the app

 int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageResource(identifier);

#5


0  

(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.) In my case I had multiple images in Drawable that I wanted to use therefore

(我每次登录时都看到这篇文章获得了更多的浏览量,我在很久以前就解决了这个问题,但我将发布这个解决方案,希望它能帮助其他人。)在我的例子中,我有多个可绘制的图像,因此我想使用它们

 for (int j=1; j<10; j++)
    {
        setImages("drawable/img" +j);
        //  SetPopup(drag);
    }

void setImages(final String draw)
{
    int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
    drag1.setImageResource(id12);

I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.

我刚刚发布了代码的一部分,帮助我直接从drawables访问不同的图像,并将它们用作多个imageViews上的拖拽图像。

#1


1  

It will be something like:

会是这样的:

R.drawable.resourcename

Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).

确保你没有Android。导入的R名称空间可能会混淆Eclipse(如果您使用的是这个名称空间)。

If that doesn't work, you can always use a context's getResources method ...

如果这不起作用,您可以始终使用上下文的getResources方法……

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);

Where this.context is intialised as an Activity, Service or any other Context subclass.

这个地方。上下文被作为活动、服务或任何其他上下文子类进行初始化。

Update:

更新:

If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?

如果它是您想要的名称,那么Resources类(由getResources()返回)具有getResourceName(int)方法,以及getResourceTypeName(int)?

Update 2:

更新2:

The Resources class has this method:

资源类有这个方法:

public int getIdentifier (String name, String defType, String defPackage) Which returns the integer of the specified resource name, type & package.

公共int getIdentifier (String name, String defType, String defPackage)返回指定资源名、类型和包的整数。

#2


0  

String uri = "@drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());

Can you try this?

你可以试试这个吗?

#3


0  

Try this,

试试这个,

final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);

#4


0  

Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.

假设您的Android资源文件夹中有几个可绘制文件,以及需要迭代它们的内容。您使用一个特定的命名模式,并希望使用它来确定资源ID。

The following steps shows how to get a resource ID based on the resource name:

下面的步骤展示了如何根据资源名获取资源ID:

   // Name of resource 
  //Type
 // Package of the app

 int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageResource(identifier);

#5


0  

(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.) In my case I had multiple images in Drawable that I wanted to use therefore

(我每次登录时都看到这篇文章获得了更多的浏览量,我在很久以前就解决了这个问题,但我将发布这个解决方案,希望它能帮助其他人。)在我的例子中,我有多个可绘制的图像,因此我想使用它们

 for (int j=1; j<10; j++)
    {
        setImages("drawable/img" +j);
        //  SetPopup(drag);
    }

void setImages(final String draw)
{
    int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
    drag1.setImageResource(id12);

I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.

我刚刚发布了代码的一部分,帮助我直接从drawables访问不同的图像,并将它们用作多个imageViews上的拖拽图像。