当前位置: 首页 > 知识库问答 >
问题:

android:src未在自定义TouchImageView中显示

乐正明辉
2023-03-14

您好,我正在使用TouchImageView

    public class NewTouchImageView extends ImageView {
    Matrix matrix = new Matrix();
    // We can be in one of these 3 states    
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // Remember some things for zooming
    PointF last = new PointF();
    PointF start = new PointF();
    float minScale = 1f;
    float maxScale = 3f;
    float[] m;

    float redundantXSpace, redundantYSpace;

    float width, height;
    static final int CLICK = 3;
    float saveScale = 1f;
    float right, bottom, origWidth, origHeight, bmWidth, bmHeight;

    ScaleGestureDetector mScaleDetector;

    Context context;
    Drawable  stub_id,mImage ;
    int stub_idd = R.drawable.loading;
    Paint paint;

    public NewTouchImageView(Context context) {
        super(context);

        sharedConstructing(context);
        this.context = context;

    }

    public NewTouchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

         this.context = context;
        sharedConstructing(context);
    }

    private void sharedConstructing(Context context) {
        super.setClickable(true);
        this.context = context;
     //   this.setImageResource(R.drawable.popupimage);
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
        matrix.setTranslate(1f, 1f);
        m = new float[9];
        setImageMatrix(matrix);
        setScaleType(ScaleType.MATRIX);

        setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                mScaleDetector.onTouchEvent(event);

                matrix.getValues(m);
                float x = m[Matrix.MTRANS_X];
                float y = m[Matrix.MTRANS_Y];
                PointF curr = new PointF(event.getX(), event.getY());

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        last.set(event.getX(), event.getY());
                        start.set(last);
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            float deltaX = curr.x - last.x;
                            float deltaY = curr.y - last.y;
                            float scaleWidth = Math.round(origWidth * saveScale);
                            float scaleHeight = Math.round(origHeight * saveScale);
                            /*if(scaleWidth>100){
                                Log.v("GREATER THAN 100", "GREATER THAN 100");
                                return false;

                            }*/
                            if (scaleWidth < width) {
                                deltaX = 0;
                                if (y + deltaY > 0)
                                    deltaY = -y;
                                else if (y + deltaY < -bottom)
                                    deltaY = -(y + bottom); 
                            } else if (scaleHeight < height) {
                                deltaY = 0;
                                if (x + deltaX > 0)
                                    deltaX = -x;
                                else if (x + deltaX < -right)
                                    deltaX = -(x + right);
                            } else {
                                if (x + deltaX > 0)
                                    deltaX = -x;
                                else if (x + deltaX < -right)
                                    deltaX = -(x + right);

                                if (y + deltaY > 0)
                                    deltaY = -y;
                                else if (y + deltaY < -bottom)
                                    deltaY = -(y + bottom);


                            }
                            matrix.postTranslate(deltaX, deltaY);
                            last.set(curr.x, curr.y);
                        }
                        break;

                    case MotionEvent.ACTION_UP:
                        mode = NONE;
                        int xDiff = (int) Math.abs(curr.x - start.x);
                        int yDiff = (int) Math.abs(curr.y - start.y);
                        if (xDiff < CLICK && yDiff < CLICK)
                            performClick();
                        break;

                    case MotionEvent.ACTION_POINTER_UP:
                        mode = NONE;
                        break;
                }
                setImageMatrix(matrix);
                invalidate();
                return true; // indicate event was handled
            }

        });
    }

    @Override
    public void setImageBitmap(Bitmap bm) { 
        super.setImageBitmap(bm);
        if(bm != null) {
            bmWidth = bm.getWidth();
            bmHeight = bm.getHeight();
        }
    }

    public void setMaxZoom(float x)
    {
        maxScale = x;
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mode = ZOOM;
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float mScaleFactor = detector.getScaleFactor();
            float origScale = saveScale;
            saveScale *= mScaleFactor;
            if (saveScale > maxScale) {
                saveScale = maxScale;
                mScaleFactor = maxScale / origScale;
            } else if (saveScale < minScale) {
                saveScale = minScale;
                mScaleFactor = minScale / origScale;
            }
            right = width * saveScale - width - (2 * redundantXSpace * saveScale);
            bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
            if (origWidth * saveScale <= width || origHeight * saveScale <= height) {
                matrix.postScale(mScaleFactor, mScaleFactor, width / 2, height / 2);
                if (mScaleFactor < 1) {
                    matrix.getValues(m);
                    float x = m[Matrix.MTRANS_X];
                    float y = m[Matrix.MTRANS_Y];
                    if (mScaleFactor < 1) {
                        if (Math.round(origWidth * saveScale) < width) {
                            if (y < -bottom)
                                matrix.postTranslate(0, -(y + bottom));
                            else if (y > 0)
                                matrix.postTranslate(0, -y);
                        } else {
                            if (x < -right) 
                                matrix.postTranslate(-(x + right), 0);
                            else if (x > 0) 
                                matrix.postTranslate(-x, 0);
                        }
                    }
                }
            } else {
                matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
                matrix.getValues(m);
                float x = m[Matrix.MTRANS_X];
                float y = m[Matrix.MTRANS_Y];
                if (mScaleFactor < 1) {
                    if (x < -right) 
                        matrix.postTranslate(-(x + right), 0);
                    else if (x > 0) 
                        matrix.postTranslate(-x, 0);
                    if (y < -bottom)
                        matrix.postTranslate(0, -(y + bottom));
                    else if (y > 0)
                        matrix.postTranslate(0, -y);
                }
            }
            return true;

        }
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        //Fit to screen.
        float scale;
        float scaleX =  (float)width / (float)bmWidth;
        float scaleY = (float)height / (float)bmHeight;
        scale = Math.min(scaleX, scaleY);
        matrix.setScale(scale, scale);
        setImageMatrix(matrix);
        saveScale = 1f;

        // Center the image
        redundantYSpace = (float)height - (scale * (float)bmHeight) ;
        redundantXSpace = (float)width - (scale * (float)bmWidth);
        redundantYSpace /= (float)2;
        redundantXSpace /= (float)2;

        matrix.postTranslate(redundantXSpace, redundantYSpace);

        origWidth = width - 2 * redundantXSpace;
        origHeight = height - 2 * redundantYSpace;
        right = width * saveScale - width - (2 * redundantXSpace * saveScale);
        bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
        setImageMatrix(matrix);
    }

}下面是各自的xml:

    <com.ifap.util.NewTouchImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:scaleType="matrix"
   android:src="@drawable/loading"
    android:contentDescription="@string/image_desc" />

在适配器获取视图:公共视图getView(int位置,视图转换视图,ViewGroup父){

    ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.icon = (NewTouchImageView) convertView
                .findViewById(R.id.image);
    } else
        holder = (ViewHolder) convertView.getTag();
imageLoader.DisplayImage(URL, holder.icon);

    return convertView;

}

ImageLoader。从这里开始的Java

在从服务器加载映像之前,如何在imageview中显示临时映像。

共有1个答案

钮勇
2023-03-14

代替android:src=“@drawable/loading”,您可以指定默认显示的临时图像名称。或者你也可以设置支架。偶像setImageBitmap(bitmapImage)。然后运行imageLoader。显示图像(URL,holder.icon);在uiThread上。因此,它会在图像下载后立即更新用户界面。

 类似资料:
  • 在FOP 2.6中,我尝试将FO文件转换成PDF。我在Windows上使用标准配置文件。 由于不支持一种图像格式,我创建了一个自定义的PreLoader、ImageLoaderFactory和ImageConverter。它们是通过服务条目注册的,从日志中我可以看到它们正在被使用: 但图像不会显示在生成的PDF文件中。。。 转换器将自定义格式转换为PNG并返回RAW_PNG: 我也曾试图皈依JPG

  • 我不知道为什么自定义文章类型的自定义分类不显示在管理列中(它消失了)。 以下是始终有效的代码: 如下所述: https://codex.wordpress.org/Function_Reference/register_taxonomy 第二个参数设置为null,因为在使用register\u post\u type()时,我将分类法与自定义post类型相关联 我不知道为什么代码停止工作了。我从3

  • 我正在尝试弹出一个自定义对话框,当我点击一个按钮,但它不会弹出在所有。我的应用程序基本上是一个日历,我将使用sqlite在日历中添加/保留约会和其他内容,使用对话框,这是指定约会细节的地方。 我为此使用的代码如下: 我做错了什么?

  • 我有自定义的消息onbeforeunload事件和工作良好,但我注意到今天它不再显示我的消息。相反,它显示“可能不会保存您所做的更改” 谁能告诉我如何修理它吗?

  • 具有自定义验证的基本HTML5表单。如果提交的值不是数字,浏览器应显示错误消息“字段必须是数字”如果输入“abc”并按submit(或按enter键),该字段将标记为无效,但不会显示错误消息。再次按submit(或按enter)将显示消息。这种双重提交行为出现在Windows和OS X上最新版本的Firefox、Chrome、Safari和IE上。您会注意到默认的“此字段是必需的…”消息在第一次提

  • 我试图在1.12.2上创建一个新的《我的世界》mod,并添加了一些基本的内容,一切都很顺利,除了自定义项目没有正确显示名称。我试图让红宝石项目进入游戏,但创意菜单显示为项目。Ruby.name。 我的错误是几乎相同的一个在这里:块纹理和块名称不加载minecraft锻造,我按照他们的建议,得到了一个lang文件,但它仍然不显示正确。我尝试了命名文件en_US. lang和en_us.lang,我尝