簡單的拖拽圖片

今天試了一下圖片拖拽辩诞,拖拽過去伴隨著自動計數(shù)功能坎弯,很簡單的小 程序。代碼如下:

布局文件
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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.example.tarena.dragimage.MainActivity">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="26dp"
        android:layout_y="31dp"
        android:id="@+id/textView2"/>

    <RelativeLayout
        android:background="@android:color/holo_blue_bright"
        android:layout_width="109dp"
        android:layout_height="162dp"
        android:layout_x="33dp"
        android:layout_y="261dp"
        android:id="@+id/leftLayout">

    </RelativeLayout>

    <RelativeLayout
        android:background="@color/colorAccent"
        android:layout_width="109dp"
        android:layout_height="162dp"
        android:layout_x="228dp"
        android:layout_y="274dp">

        <RelativeLayout
            android:background="@color/colorAccent"
            android:layout_width="109dp"
            android:layout_height="162dp"
            android:layout_x="229dp"
            android:layout_y="264dp">

            <RelativeLayout
                android:background="@color/colorAccent"
                android:layout_width="109dp"
                android:layout_height="162dp"
                android:layout_x="50dp"
                android:layout_y="252dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="26dp"
                android:id="@+id/rightLayout">

            </RelativeLayout>
        </RelativeLayout>

    </RelativeLayout>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="281dp"
        android:layout_y="35dp"
        android:id="@+id/textView3"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="77dp"
        android:layout_y="112dp"
        android:id="@+id/imageView2"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="166dp"
        android:layout_y="110dp"
        android:id="@+id/imageView"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="257dp"
        android:layout_y="111dp"
        android:id="@+id/imageView3"/>```


#####MainActivity
@BindViews({R.id.imageView, R.id.imageView2, R.id.imageView3})
List<ImageView> imageviews;

@BindView(R.id.textView2)
TextView leftCountTV;
@BindView(R.id.textView3)
TextView rightCountTV;
@BindView(R.id.activity_main)
AbsoluteLayout activityMain;
@BindView(R.id.leftLayout)
RelativeLayout leftLayout;
@BindView(R.id.rightLayout)
RelativeLayout rightLayout;
private ImageView dragIV;

int leftCount;
int rightCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // 隱藏狀態(tài)欄
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
            .LayoutParams.FLAG_FULLSCREEN);


    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            for (ImageView imageview : imageviews) {
                Rect r = new Rect();
                imageview.getGlobalVisibleRect(r);

                String s = "";


                if (r.contains((int) event.getRawX(), (int) event.getRawY())) {

                    dragIV = new ImageView(this);
                    //獲取點擊圖片的bitmap
                    Bitmap b = ((BitmapDrawable) imageview.getDrawable()).getBitmap();
                    dragIV.setImageBitmap(b);
                    dragIV.setLayoutParams(imageview.getLayoutParams());
                    activityMain.addView(dragIV);
                }


            }

            break;
        case MotionEvent.ACTION_MOVE:

            if (dragIV != null) {
                dragIV.setX(event.getRawX()-dragIV.getWidth()/2);
                dragIV.setY(event.getRawY()-dragIV.getHeight()/2);
            }

            break;
        case MotionEvent.ACTION_UP:

            if (dragIV!=null) {
                Rect leftRect = new Rect();
                Rect rightRect = new Rect();
                leftLayout.getGlobalVisibleRect(leftRect);
                rightLayout.getGlobalVisibleRect(rightRect);
                if (leftRect.contains((int)event.getRawX(),(int)event.getRawY())) {

                leftCountTV.setText("" + ++leftCount);

                }else if (rightRect.contains((int)event.getRawX(),(int)event.getRawY())) {

                    rightCountTV.setText("" + ++rightCount);


                }else{//在外面松手
                    activityMain.removeView(dragIV);
                }
            }

            break;
    }


    return super.onTouchEvent(event);
}```
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末译暂,一起剝皮案震驚了整個濱河市抠忘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌外永,老刑警劉巖崎脉,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異伯顶,居然都是意外死亡囚灼,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門祭衩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灶体,“玉大人,你說我怎么就攤上這事掐暮⌒椋” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵路克,是天一觀的道長樟结。 經(jīng)常有香客問我养交,道長,這世上最難降的妖魔是什么瓢宦? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任碎连,我火速辦了婚禮,結果婚禮上刁笙,老公的妹妹穿的比我還像新娘破花。我一直安慰自己谦趣,他們只是感情好疲吸,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著前鹅,像睡著了一般摘悴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舰绘,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天蹂喻,我揣著相機與錄音,去河邊找鬼捂寿。 笑死口四,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的秦陋。 我是一名探鬼主播蔓彩,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼驳概!你這毒婦竟也來了赤嚼?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤顺又,失蹤者是張志新(化名)和其女友劉穎更卒,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稚照,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡蹂空,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了果录。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腌闯。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖雕憔,靈堂內(nèi)的尸體忽然破棺而出姿骏,到底是詐尸還是另有隱情,我是刑警寧澤斤彼,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布分瘦,位于F島的核電站蘸泻,受9級特大地震影響,放射性物質發(fā)生泄漏嘲玫。R本人自食惡果不足惜悦施,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望去团。 院中可真熱鬧抡诞,春花似錦、人聲如沸土陪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鬼雀。三九已至顷窒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間源哩,已是汗流浹背鞋吉。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留励烦,地道東北人谓着。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像坛掠,于是被迫代替她去往敵國和親赊锚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容