Android 检查是否可拖动';s对象和drop';s区域标记相等
我正在做一个益智游戏 我从数组中随机选择一个图像,并将其分成4部分 我的布局由3个表格行组成 第一行是线性布局,也有两行。此布局将随机选择的图像作为背景,透明度为0.3 第二行和第三行仅用于分割图像的4个部分 因此,我认为我的问题和我迄今为止的描述已经清楚地说明了我的问题Android 检查是否可拖动';s对象和drop';s区域标记相等,android,drag-and-drop,Android,Drag And Drop,我正在做一个益智游戏 我从数组中随机选择一个图像,并将其分成4部分 我的布局由3个表格行组成 第一行是线性布局,也有两行。此布局将随机选择的图像作为背景,透明度为0.3 第二行和第三行仅用于分割图像的4个部分 因此,我认为我的问题和我迄今为止的描述已经清楚地说明了我的问题 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:ori
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="@+id/adView"
android:weightSum="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="7dp"
android:layout_weight=".5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/puzzleBackground"
android:weightSum="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight=".5">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:id="@+id/piece1"
android:tag="piece1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:id="@+id/piece2"
android:tag="piece2"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight=".5">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:id="@+id/piece3"
android:tag="piece3"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:id="@+id/piece4"
android:tag="piece4"
/>
</TableRow>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".5"
android:id="@+id/part1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight=".5"
android:id="@+id/part2" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".25"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".5"
android:id="@+id/part3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight=".5"
android:id="@+id/part4" />
</TableRow>
</LinearLayout>
公共类谜题扩展了AppCompative活动{
私有字符串[]imgs;
私有字符串随机化;
私有图像视图第1部分、第2部分、第3部分、第4部分;
私有文本视图片段1、片段2、片段3、片段4;
私人线路布局背景;
私有位图bm1、bm2、bm3、bm4;
private List parts=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_拼图);
//选择随机图像
拼图imgs=getResources().getStringArray(R.array.all\u animal\u imgs);
RandomPuzzImg=PuzzImgs[new Random().nextInt(puzzImgs.length)];
//获取元素
part1=(ImageView)findViewById(R.id.part1);
part2=(ImageView)findViewById(R.id.part2);
第3部分=(ImageView)findViewById(R.id.part3);
第4部分=(ImageView)findViewById(R.id.part4);
piece1=(TextView)findViewById(R.id.piece1);
piece2=(TextView)findViewById(R.id.piece2);
piece3=(TextView)findViewById(R.id.piece3);
piece4=(TextView)findViewById(R.id.piece4);
第1部分:setOnTouchListener(新的MyTouchListener());
part2.setOnTouchListener(新的MyTouchListener());
第3部分:setOnTouchListener(新的MyTouchListener());
第4部分:setOnTouchListener(新的MyTouchListener());
piece1.setOnDragListener(新的MyDragListener());
piece2.setOnDragListener(新的MyDragListener());
piece3.setOnDragListener(新的MyDragListener());
piece4.setOnDragListener(新的MyDragListener());
//将随机选择的资源图像转换为位图
位图originalBm=BitmapFactory.decodeResource(getResources(),getImageId(this,randomPuzzleIMG));
//将位图拆分为4个部分
bm1=Bitmap.createBitmap(originalBm,0,0,(originalBm.getWidth()/2),(originalBm.getHeight()/2));
bm2=Bitmap.createBitmap(originalBm,(originalBm.getWidth()/2),0,(originalBm.getWidth()/2),(originalBm.getHeight()/2));
bm3=Bitmap.createBitmap(originalBm,0,(originalBm.getHeight()/2),(originalBm.getWidth()/2),(originalBm.getHeight()/2));
bm4=Bitmap.createBitmap(originalBm,(originalBm.getWidth()/2),(originalBm.getHeight()/2),(originalBm.getWidth()/2),(originalBm.getHeight()/2));
//使背景透明
piece1.setBackgroundDrawable(新的BitmapDrawable(getResources(),bm1));
第1段:setAlpha(0.3f);
piece2.setBackgroundDrawable(新的BitmapDrawable(getResources(),bm2));
第2段:setAlpha(0.3f);
piece3.setBackgroundDrawable(新的BitmapDrawable(getResources(),bm3));
第3段:setAlpha(0.3f);
piece4.setBackgroundDrawable(新的BitmapDrawable(getResources(),bm4));
第4段:setAlpha(0.3f);
//将零件放置在阵列中
部分。添加(bm1);
部分。添加(bm2);
部分。添加(bm3);
部分。添加(bm4);
//洗牌
收藏。洗牌(部分);
//为每个零件指定正确的工件标签
对于(inti=0;i噢,我刚解出来
我唯一要做的就是将DragEvent.ACTION的if语句嵌套在另一个if语句中,如下所示
if(view.getTag().equals(v.getTag())){
if(view.getTag().equals("piece1")){
owner.removeView(view);
piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1));
piece1.setAlpha(0.9f);
} else if (view.getTag().equals("piece2")){
owner.removeView(view);
piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2));
piece2.setAlpha(0.9f);
} else if (view.getTag().equals("piece3")){
owner.removeView(view);
piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3));
piece3.setAlpha(0.9f);
} else if (view.getTag().equals("piece4")) {
owner.removeView(view);
piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4));
piece4.setAlpha(0.9f);
}
}
if(view.getTag().equals(v.getTag())){
if(view.getTag().equals("piece1")){
owner.removeView(view);
piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1));
piece1.setAlpha(0.9f);
} else if (view.getTag().equals("piece2")){
owner.removeView(view);
piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2));
piece2.setAlpha(0.9f);
} else if (view.getTag().equals("piece3")){
owner.removeView(view);
piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3));
piece3.setAlpha(0.9f);
} else if (view.getTag().equals("piece4")) {
owner.removeView(view);
piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4));
piece4.setAlpha(0.9f);
}
}