[英]android how do I update the view to include a custom graphic?
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). 我不太了解该类的使用方式(自学Java编码器,因此这里可能缺少一些基础知识)。 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. 我可以使用标准ImageView来完成所有这些操作,但是使用ImageView我无法缩放/移动。
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. 主类,它仅使用touchexampleview类将图像放置在屏幕上,而touchexampleview类包含加载默认图像的一行。
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 和touchexampleview类
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();
}
}
}
我不确定,但这是您要寻找的吗?
imageSpot.setImageResource(R.drawable.myImage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.