你的android我怎么更新视图,包括自定义图形? [英] android how do I update the view to include a custom graphic?

查看:118
本文介绍了你的android我怎么更新视图,包括自定义图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这真的很有用code,让我扩展和移动位图,我一直试图找到一种方法,在我的应用程序使用此code。我真的不明白这个类所使用的方式(自学Java的codeR,所以我可以在这里失去了一些基础知识)。基本上,我希望能够开拓库,并挑选可以通过此code使用的文件。我可以做这一切与标准的ImageView但有一个ImageView的我无法缩放/移动。

的主类,它简单地使用touchexampleview类将图像放在屏幕上,并touchexampleview类包含加载默认的图像一行。

 米康= context.getResources()getDrawable(R.drawable.ic_launcher)。

我可以工作,如何在课堂上改变这一点,但我希望能够动态地做到这一点,是用户驱动。

下面是主类。

 公共类TouchExampleActivity延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        TouchExampleView视图=新TouchExampleView(本);        view.setLayoutParams(新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));        的setContentView(视图);    }
}

和touchexampleview类

 公共类TouchExampleView扩展视图{
    私人可绘制米康;
    私人浮动mPosX;
    私人浮动mPosY;    私人VersionedGestureDetector mDetector;
    私人浮动mScaleFactor = 1.F;    公共TouchExampleView(上下文的背景下){
        这(背景下,NULL,0);
    }    公共TouchExampleView(上下文的背景下,ATTRS的AttributeSet){
        这(背景下,ATTRS,0);
    }    公共TouchExampleView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);        米康= context.getResources()getDrawable(R.drawable.ic_launcher)。
        mIcon.setBounds(0,0,mIcon.getIntrinsicWidth(),mIcon.getIntrinsicHeight());        mDetector = VersionedGestureDetector.newInstance(上下文,新GestureCallback());
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent EV){
        mDetector.onTouchEvent(EV);
        返回true;
    }    @覆盖
    公共无效的onDraw(帆布油画){
        super.onDraw(画布);        canvas.save();
        canvas.translate(mPosX,mPosY);
        canvas.scale(mScaleFactor,mScaleFactor);
        mIcon.draw(画布);
        canvas.restore();
    }    私有类GestureCallback实现VersionedGestureDetector.OnGestureListener {
        公共ondrag当无效(DX浮动,浮动DY){
            mPosX + = DX;
            mPosY + =颐;
            无效();
        }        公共无效onScale(浮动比例因子){
            mScaleFactor * =比例因子;            //不要让对象获取过小或过大。
            mScaleFactor = Math.max(0.1F,Math.min(mScaleFactor,5.0F));            无效();
        }
    }
}


解决方案

我不知道,但是这是你在找什么?

  imageSpot.setImageResource(R.drawable.myImage);

I found this really useful code that allows me to scale and move a bitmap and I've been trying to find a way to use this code in my app. I don't really understand the way the class is being used (self taught java coder, so I may be missing some basics here). I basically want to be able to open up gallery and pick a file which can be used by this code. I can do all this with a standard ImageView but with an ImageView I'm unable to scale/move.

The main class, and it simply uses the touchexampleview class to place the image on screen, and the touchexampleview class contains a line which loads a default image.

mIcon = context.getResources().getDrawable(R.drawable.ic_launcher);

I could work out how to change this in the class but I want to be able to do this dynamically and be user driven.

Here is the main class.

public class TouchExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TouchExampleView view = new TouchExampleView(this);

        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        setContentView(view);

    }
}

and the touchexampleview class

public class TouchExampleView extends View {
    private Drawable mIcon;
    private float mPosX;
    private float mPosY;

    private VersionedGestureDetector mDetector;
    private float mScaleFactor = 1.f;

    public TouchExampleView(Context context) {
        this(context, null, 0);
    }

    public TouchExampleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mIcon = context.getResources().getDrawable(R.drawable.ic_launcher);
        mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());

        mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mDetector.onTouchEvent(ev);
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
        mIcon.draw(canvas);
        canvas.restore();
    }

    private class GestureCallback implements VersionedGestureDetector.OnGestureListener {
        public void onDrag(float dx, float dy) {
            mPosX += dx;
            mPosY += dy;
            invalidate();
        }

        public void onScale(float scaleFactor) {
            mScaleFactor *= scaleFactor;

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

            invalidate();
        }
    }
}

解决方案

I am not sure, but is this what you are looking for?

imageSpot.setImageResource(R.drawable.myImage);

这篇关于你的android我怎么更新视图,包括自定义图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆