Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中绘制位图-将内容包装到曲面视图_Android_Bitmap_Surfaceview - Fatal编程技术网

在Android中绘制位图-将内容包装到曲面视图

在Android中绘制位图-将内容包装到曲面视图,android,bitmap,surfaceview,Android,Bitmap,Surfaceview,我创建了一个带有线程的自定义曲面视图类,并在相对布局的主活动中添加了自定义曲面视图。现在,我在自定义曲面视图类构造函数中声明一个位图 public static Bitmap mbitmap; public Surface(Context ctx, AttributeSet attrSet) { super(ctx, attrSet); context = ctx; mbitmap = BitmapFactory.decodeResource(co

我创建了一个带有线程的自定义曲面视图类,并在相对布局的主活动中添加了自定义曲面视图。现在,我在自定义曲面视图类构造函数中声明一个位图

public static Bitmap mbitmap;
public Surface(Context ctx, AttributeSet attrSet) {
        super(ctx, attrSet);
        context = ctx;
        mbitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.abc);         
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
    }
我把它画在画布上

public void doDraw(Canvas canvas) {
    canvas.drawBitmap(mbitmap, 0, 0, null);
}
我画得很成功。但它占据了整个屏幕

<RelativeLayout
    android:id="@+id/outer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 <com.cg.Surface
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
       <RelativeLayout
        android:id="@+id/processing_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>
</RelativeLayout>

如果没有,如何在位图中使用setMeasuredDimension并限制surfaceview的大小

我们可以使用

public void drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint); 
我的意思是:

canvas.drawBitmap(bitmap, src, dst, paint);
onDraw中的方法

Rect src = new Rect(0, 0, width, height);
Rect dst = new Rect(x, y, x + width, y + height); 

我希望能做到。我想这可能会对其他人有所帮助,因为我一直在等待自己的解决方案。

我们可以使用矩形,然后使用矩阵在其中绘制图像,这样我们就可以实现

public void doDraw(Canvas canvas)
{
int width = mbitmap.getWidth();
int height = mbitmap.getHeight();
float h= (float) height;
float w= (float) width;
Matrix mat=new Matrix();
mat.setTranslate( 500, 500 );
mat.setScale(800/w ,800/h);
canvas.drawBitmap(mbitmap, mat, null);
}
public void doDraw(Canvas canvas)
{
int width = mbitmap.getWidth();
int height = mbitmap.getHeight();
float h= (float) height;
float w= (float) width;
Matrix mat=new Matrix();
mat.setTranslate( 500, 500 );
mat.setScale(800/w ,800/h);
canvas.drawBitmap(mbitmap, mat, null);
}