基本上我想在图像上绘制线条,以便在缩放图像时,线条也会按比例缩放,或任何类型的交互,如投掷或缩放重置。我使用TouchImageView作为我的ImageView,这是我的代码
public class ParkA extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parka);
TouchImageView parka = (TouchImageView) findViewById(R.id.parka);
parka.setImageResource(R.drawable.parka);
Bitmap lineABmp = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
Canvas lineACanvas = new Canvas(lineABmp);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
lineACanvas.drawBitmap(lineABmp,0 ,0, null);
//lineACanvas.drawLine(60, 64, 60, 500, paint);
TouchImageView linea = new TouchImageView(this);
linea = (TouchImageView) findViewById(R.id.parka);
linea.setImageBitmap(lineABmp);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
}
@Override
public void onBackPressed() {
//Do Nothing
}
}
这是我的xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kmparkit.ParkA" >
<com.km.parkit.TouchImageView
android:id="@+id/parka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:contentDescription="@string/imageDesc"
android:scaleType="matrix" />
<!-- android:src="@drawable/parka" -->
</RelativeLayout>
如果我更改Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
进入Bitmap.createBitmap(parka.getWidth(), parka.getHeight(), Bitmap.Config.ARGB_8888);
logcat说宽度和高度必须> 0
如果我drawLine,只会显示绿线本身
为了创建一个带有线条的TouhImageView图片,我在这里缺少什么?
更新:
这是解决方案!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parka);
TouchImageView parka = (TouchImageView) findViewById(R.id.parka);
parka.setImageResource(R.drawable.parka);
Bitmap lineABmp = ((BitmapDrawable)parka.getDrawable()).getBitmap();
Bitmap lineAMutBmp = lineABmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas lineACanvas = new Canvas(lineAMutBmp);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
lineACanvas.drawLine(82, 1058, 82, 858, paint);
parka.setImageBitmap(lineAMutBmp);
}
我必须复制位图才能使其变为可变并使用parka.setImageResource();
和parka.setImageBitmap()
在TouchImageView
中显示图片和线条,同时使用{{1}绘制线条}}
答案 0 :(得分:1)
尝试这样的事情:
编辑:这应该可以解决问题;)
setContentView(R.layout.parka);
TouchImageView parka = (TouchImageView) findViewById(R.id.parka);
parka.setImageResource(R.drawable.parka);
Bitmap lineABmp = ((BitmapDrawable)parka.getDrawable()).getBitmap();
Bitmap copy = Bitmap.createBitmap(lineABmp);
Canvas lineACanvas = new Canvas(copy);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
lineACanvas.drawLine(60, 64, 60, 500, paint);
parka.setImageBitmap(copy);
快乐编码:D