如何以声明方式设置 contentview 或使用 customview 类

发布于 2024-10-30 09:51:47 字数 3982 浏览 0 评论 0原文

我想将 SD 卡中的图片加载到我的视图中,然后在其上绘图。到目前为止,我可以在位图上绘制一个圆圈后拍摄照片并将其显示在图像视图中,但这不是应该这样做的。我认为我需要创建一个自定义视图类并覆盖它的 onDraw()。 Atm 我有一个带有表面视图的相对布局,在表面视图之后我有图像视图和按钮。我的问题是,如果我编写一个自定义视图类来处理绘图,然后我会将其传递给我的活动的 setContent 视图,令我困惑的是,如果通过 xml 文件以声明方式声明布局,我如何设置活动的 setContentView( )到处理绘图的自定义视图类?

谢谢 mat

[编辑]

这是我正在使用的 xml 布局。这不是我应该如何使用图像视图来做到这一点。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="match_parent" 
                android:layout_height="wrap_content" android:gravity="fill">



    <ImageView android:id="@+id/imageView1" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"></ImageView>

               <com.tecmark.HorizontalSlider android:id="@+id/slider"
                android:indeterminateOnly="false"
                android:progressDrawable="@android:drawable/progress_horizontal"
                android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
                android:minHeight="20dip"
                android:maxHeight="20dip" android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>



 </RelativeLayout>               




public class LoadPic extends Activity{

    private static final String TAG = "Loadpic";
    private ImageView imageview;
    private File tempFile;
    private byte[] imageArray;
    private HorizontalSlider slider;
    private Canvas canvas;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, " loadpic onCreate");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.load2);
        setProgressBarVisibility(true);
        slider = (HorizontalSlider) this.findViewById(R.id.slider);
        slider.setOnProgressChangeListener(changeListener);


        imageview = (ImageView)findViewById(R.id.imageView1);

        tempFile = new File(Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/"+"image.jpg");

        imageArray = new byte[(int)tempFile.length()];

  try{

        InputStream is = new FileInputStream(tempFile);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);


        int i = 0;

        while (dis.available() > 0) {
        imageArray[i] = dis.readByte();
        i++;
        }

        dis.close();

  } catch (Exception e) {

           e.printStackTrace();
        }

        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 5;
        Bitmap bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
        Log.e(TAG, bm.toString());
        //imageview.setImageBitmap(bm);


        Bitmap bmOverlay = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
        canvas = new Canvas(bmOverlay);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.STROKE);



        canvas.drawBitmap(bm, new Matrix(), null);
        canvas.drawCircle(50, 50, 25, paint);
        imageview.setImageBitmap(bmOverlay);






    }// end of onCreate


    private OnProgressChangeListener changeListener = new OnProgressChangeListener() {




                    @Override
                    public void onProgressChanged(View v, int progress) {
                        // TODO Auto-generated method stub
                        setProgress(progress);

                    }



            };


}//end of Activity

我正在考虑做的是创建一个自定义视图类来处理位图上的绘图。但如果我这样做,那么当它指向 xml 文件时,如何使 setContentView() 指向我的 customView 类。抱歉,如果我在这里遗漏了一些东西。

i'd like to load a picture from sdcard into my view then draw upon it. so far i can take the picture and display it in an imageview after i've drawn a circle on the bitmap but this is not how it should be done. I think that i need to create a custom view class and override it's onDraw(). Atm i have a relative layout with a surfaceview, after the surfaceview i have the imageview and buttons. My question is if i write a custom view class to handle the drawing, i would then pass that to my activity's setContent view, what is confusing me is if the layout is declared declaratively via the xml file, how can i set the activity's setContentView() to the custom view class that handles the drawing?

thanks mat

[edit]

here's the xml layout i'm using. this is not how i should do it using an imageview.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="match_parent" 
                android:layout_height="wrap_content" android:gravity="fill">



    <ImageView android:id="@+id/imageView1" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"></ImageView>

               <com.tecmark.HorizontalSlider android:id="@+id/slider"
                android:indeterminateOnly="false"
                android:progressDrawable="@android:drawable/progress_horizontal"
                android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
                android:minHeight="20dip"
                android:maxHeight="20dip" android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>



 </RelativeLayout>               




public class LoadPic extends Activity{

    private static final String TAG = "Loadpic";
    private ImageView imageview;
    private File tempFile;
    private byte[] imageArray;
    private HorizontalSlider slider;
    private Canvas canvas;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, " loadpic onCreate");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.load2);
        setProgressBarVisibility(true);
        slider = (HorizontalSlider) this.findViewById(R.id.slider);
        slider.setOnProgressChangeListener(changeListener);


        imageview = (ImageView)findViewById(R.id.imageView1);

        tempFile = new File(Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/"+"image.jpg");

        imageArray = new byte[(int)tempFile.length()];

  try{

        InputStream is = new FileInputStream(tempFile);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);


        int i = 0;

        while (dis.available() > 0) {
        imageArray[i] = dis.readByte();
        i++;
        }

        dis.close();

  } catch (Exception e) {

           e.printStackTrace();
        }

        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 5;
        Bitmap bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
        Log.e(TAG, bm.toString());
        //imageview.setImageBitmap(bm);


        Bitmap bmOverlay = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
        canvas = new Canvas(bmOverlay);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.STROKE);



        canvas.drawBitmap(bm, new Matrix(), null);
        canvas.drawCircle(50, 50, 25, paint);
        imageview.setImageBitmap(bmOverlay);






    }// end of onCreate


    private OnProgressChangeListener changeListener = new OnProgressChangeListener() {




                    @Override
                    public void onProgressChanged(View v, int progress) {
                        // TODO Auto-generated method stub
                        setProgress(progress);

                    }



            };


}//end of Activity

What i'm thinking of doing is creating a customview class that handles the drawing on the bitmap. But if i do this then how do i make setContentView() to point to my customView class when it point to the xml file. Sorry if i missing something here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-11-06 09:51:47

如果您想要位图和绘图的自定义视图,那么您可以简单地扩展 ImageView 并覆盖 Paint 方法。像这样的事情:

public class MyImage extends ImageView {
    ...
    @Override
    public void paint(...){}
}

在 xml 布局中将 ImageView 更改为

<what.ever.package.MyImage ... />

If you want to have a custom view for the bitmap and drawing then you can simply extend ImageView and override the paint method. Something like that:

public class MyImage extends ImageView {
    ...
    @Override
    public void paint(...){}
}

And in the xml layout change the ImageView to

<what.ever.package.MyImage ... />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文