简体   繁体   English

如何从ImageView获取上下文

[英]How to get a Context from an ImageView

I am trying to make an ImageView be a surface that I can draw on with my finger. 我正在尝试使ImageView成为我可以用手指绘制的表面。

I have followed Android 2D Graphics Example , which resulted in making a whole screen drawable (with some issues, discussed in question 8287949 , but somewhat disconnected from what I am trying to do.) 我遵循了Android 2D图形示例 ,该示例使整个屏幕都可绘制(存在一些问题,在问题8287949中进行了讨论,但与我尝试执行的操作有些脱节。)

I am working with: 我正在与:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceStatA);
    // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    drawView = new DrawView(this);
    drawView.requestFocus();
    setContentView(R.layout.activity_main);
}

I'm thinking I need to change drawView = new DrawView(this) to something that would reference the Context of the resource I define in activity_main.xml : 我在想我需要将drawView = new DrawView(this)更改为引用我在activity_main.xml定义的资源的上下文的内容:

ImageView
    android:id="@+id/canvasview"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnRed"
    android:background="@drawable/kitty"

However, I either don't know what I'm looking for in the API reference, or I'm not finding it. 但是,我或者不知道我在API参考中正在寻找什么,或者找不到它。

The class for DrawView is documented at Android 2D Graphics Example , so I won't repeat it here. DrawView的类已在Android 2D Graphics Example中进行了记录 ,因此在此不再赘述。

I have been working through AppInventor, and trying to duplicate what I learn there in Eclipse using the real Android SDK, but I'm feeling stumped on Tutorial #1, so sorry for the newbie question! 我一直在研究AppInventor,并尝试使用真正的Android SDK复制我在Eclipse中学到的东西,但是我对教程1感到困惑,所以对新手问题感到抱歉!

The Context used to create the View is actually the Activity so it's already assigned to the this reference. 用于创建ViewContext实际上是Activity因此已经将其分配给this引用。 It's set when you call setContentView() . 在调用setContentView()时设置。

Also, View#getContext() . 另外, View#getContext()

So to retrieve the Context you would to get a reference to the View then call getContext() . 因此,要检索Context您将获取对View的引用,然后调用getContext() In this specific case, it would be 在这种情况下,

ImageView img = (ImageView) findViewById(R.id.canvasview);
Context c = img.getContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM