我有一个项目,我需要一个像视图一样的自定义画布(白板)。
活动将有两个部门
上述画布。
包含图片的列表/回收站视图(忽略临时LinearLayout而非list / recycler视图)
所以当我点击图片时。此图像将传输到画布。用户可以拖动,缩放,旋转或将所有这些一起拖动到该图像。此画布应同时支持最多4-5张图像。
我想知道是否有更好的选择,或者我正朝着正确的方向前进。
如果我走向正确的方向。请帮我解决这个问题。
A / libc:致命信号11(SIGSEGV),代码1,故障地址0x180,分别为18318(odrob_sylvester)
这是我的活动类( CanvasActivity.java )
public class CanvasActivity extends AppCompatActivity {
private static final String TAG = CanvasActivity.class.getSimpleName();
private WodrobView canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_canvas);
canvas = (WodrobView) findViewById(R.id.canvas);
}
public void img1click(View view) {
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.wodrob_1);
Product product = new Product(image);
Details details = new Details("Wodrob_1", "123", 123);
product.setDetails(details);
WodrobHandler.addObjectToWODROB(product);
Log.d(TAG, "img1click called");
canvas.updateCanvas();
}
}
activity_canvas.xml
<LinearLayout 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:orientation="vertical"
tools:context="com.example.wodrob.wodrob_sylvester.CanvasActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/card_margin_horizontal"
android:background="#ffffff"
android:elevation="@dimen/canvas_elevation">
<com.example.wodrob.wodrob_sylvester.widget.WodrobView
android:id="@+id/canvas"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="250dp" />
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/card_margin_horizontal"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:clickable="true"
android:scaleType="fitXY"
android:background="@color/black"
android:padding="1dp"
android:onClick="img1click"
android:src="@drawable/wodrob_1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:background="@color/black"
android:padding="1dp"
android:src="@drawable/wodrob_2" />
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:background="@color/black"
android:padding="1dp"
android:src="@drawable/wodrob_3" />
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:background="@color/black"
android:padding="1dp"
android:src="@drawable/wodrob_4" />
</LinearLayout>
</ScrollView>
</LinearLayout>
自定义小部件画布( WodrobView.java )
public class WodrobView extends View {
private static final String TAG = WodrobView.class.getSimpleName();
private Context mContext;
public WodrobView(Context context) {
super(context);
mContext = context;
}
public WodrobView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public WodrobView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
@Override
public void draw(final Canvas canvas) {
super.draw(canvas);
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
drawingImages(canvas);
}
});
}
private void drawingImages(Canvas canvas){
Map<Point, Product> drawMap = WodrobHandler.getObjects();
for (Point point : drawMap.keySet()){
Product product = drawMap.get(point);
if (product.isAdded()) {
Log.w(TAG, product.getDetails().getName() + " is already added!");
continue;
}
Bitmap bitmap = product.getBitmap();
Bitmap bitmapTemp = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 2, bitmap.getHeight() / 2, false);
canvas.drawBitmap(bitmapTemp, point.x, point.y, new Paint(Paint.ANTI_ALIAS_FLAG));
}
}
public void updateCanvas() {
invalidate();
}
}
WodrobHandler.java
public class WodrobHandler implements WodrobHelper {
interface WodrobHelper {
Point[] POINTS = new Point[]{
new Point(10, 10),
new Point(10, 10),
new Point(10, 10),
new Point(10, 10)
};
}
private static final String TAG = WodrobHandler.class.getSimpleName();
private static Map<Point, Product> objects = new HashMap<>();
private static short position = 0;
public static boolean addObjectToWODROB(Product product){
if (position >= 4 || position < 0){
Set<Point> pointSet = objects.keySet();
for (Point point:POINTS) {
short flag = 0;
for (Point point1:pointSet) {
if (point.equals(point1.x, point1.y))
flag++;
}
if (flag == 0) {
objects.put(point, product);
return true;
}
}
Log.e(TAG, "Invalid position or full");
return false;
} else {
objects.put(POINTS[position++], product);
return true;
}
}
public static boolean removeObjectFromWODROB(Product product){
if (!objects.containsValue(product)){
Log.e(TAG, "No such product to remove");
return false;
}
objects.remove(product.getPoint());
return true;
}
public static Map<Point, Product> getObjects() {
return objects;
}
public static void setObjects(Map<Point, Product> objects) {
WodrobHandler.objects = objects;
}
}
Product.java
public class Product {
private Bitmap bitmap;
private Details details;
Point point = null;
private boolean added = false;
public boolean isAdded() {
return added;
}
public void setAdded(boolean added) {
this.added = added;
}
public Product(Bitmap bitmap) {
//Getting bitmap from drawable resource
this.bitmap = bitmap;
}
public Bitmap getBitmap() {
return bitmap;
}
public Details getDetails() {
return details;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public void setDetails(Details details) {
this.details = details;
}
}
Details.java
public class Details {
private String name, id;
private int cost;
public Details(String name, String id, int cost) {
this.name = name;
this.id = id;
this.cost = cost;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public int getCost() {
return cost;
}
}
答案 0 :(得分:0)
我认为objects
中的WodrobHandler
为空。您添加元素的代码在哪里?
您必须将位图转换为可变,并避免在onDraw中创建位图。在Construtor上创建它。代码创建位图可变:
Bitmap. createScaledBitmap(souce,souce.getWidth(),source.getHeight,false);