我想将sdcard中的图片加载到我的视图中然后绘制它。到目前为止,我可以拍摄照片,并在我在位图上绘制一个圆圈之后将其显示在图像视图中,但这不是应该如何完成的。我认为我需要创建一个自定义视图类并覆盖它的onDraw()。 Atm我有一个带有surfaceview的相对布局,在surfaceview之后我有了imageview和按钮。我的问题是,如果我编写一个自定义视图类来处理绘图,我会将其传递给我的activity的setContent视图,令我困惑的是如果布局是通过xml文件以声明方式声明的,我该如何设置activity的setContentView( )到处理绘图的自定义视图类?
感谢垫
[编辑]
这是我正在使用的xml布局。这不是我应该如何使用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
我正在考虑做的是创建一个处理位图绘图的customview类。但是,如果我这样做,那么当指向xml文件时,如何使setContentView()指向我的customView类。对不起,如果我在这里遗漏了什么。
答案 0 :(得分:0)
如果要为位图和绘图创建自定义视图,则只需扩展ImageView并覆盖绘制方法即可。这样的事情:
public class MyImage extends ImageView {
...
@Override
public void paint(...){}
}
在xml布局中将ImageView更改为
<what.ever.package.MyImage ... />