Android ImageView点击选中后添加边框

ImageView点击选中后添加边框是最近做连连看时遇到的一个小需求,从网上搜了一下,发现有用Selector做的,有用Canvas做的,但是它们都没能满足我的需求。所以,我就思考寻找其他的解决方法,经过尝试找到了一种实现的方法,具体的方法如下:1、制作一个和ImageView中要显示的图片一样大小的透明图片。2、通过绘图工具给透明图片画上边框。3、把ImageView要显示的图片设置成backgroud。4、给ImageView添加点击事件处理,动态将带边框的透明图片加入ImageView。下面就把代码和效果图展示给大家。

 

1. MainActivity.java

package com.htt.lianliankangame;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

import static com.htt.lianliankangame.R.*;
import static com.htt.lianliankangame.R.drawable.background_border;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    GridLayout gridView;
    ImageView imageView;
    ImageView currentImage;
    int dpScale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(id.toolbar);
        setSupportActionBar(toolbar);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        dpScale = (int)metrics.density;

        FloatingActionButton fab = (FloatingActionButton) findViewById(id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        //动态绘制64个带不同图片的网格
        addImageView();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.Game_difficult) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void addImageView(){
        gridView = (GridLayout)findViewById(id.gridLayout);
        for(int i = 0; i < 64; i++){
            imageView = new ImageView(this);
            //为每一个网格设置一个ID,然后给每一个网格附随机的图片,网格与图片之间是相互分离的
            imageView.setId(i);
            //随机产生一个数,然后通过这个数去获取图片
            int random = getRandom();
            switch (random){
                case 1:
                    imageView.setBackgroundResource(drawable.icon_1);
                    break;
                case 2:
                    imageView.setBackgroundResource(drawable.icon_2);
                    break;
                case 3:
                    imageView.setBackgroundResource(drawable.icon_3);
                    break;
                case 4:
                    imageView.setBackgroundResource(drawable.icon_4);
                    break;
                case 5:
                    imageView.setBackgroundResource(drawable.icon_5);
                    break;
                case 6:
                    imageView.setBackgroundResource(drawable.icon_6);
                    break;
                case 7:
                    imageView.setBackgroundResource(drawable.icon_7);
                    break;
                case 8:
                    imageView.setBackgroundResource(drawable.icon_8);
                    break;
            }
            ViewGroup.LayoutParams params = new GridLayout.LayoutParams();
            imageView.setLayoutParams(new LinearLayout.LayoutParams(125,125));
            imageView.setClickable(true);
            //为每一个imageView设置监听事件
            imageView.setOnClickListener(this);
            gridView.addView(imageView);
        }
    }

    public int getRandom(){
        Random random = new Random();//定义随机类
        int result = random.nextInt(8);
        return result+1;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        handleImageView((ImageView)v);

    }

    public void handleImageView(ImageView imageView){
        imageView.setImageResource(drawable.background_border);
        currentImage = imageView;
    }


}

2. background_border.xml



    
    
    
    

3. 运行后效果图

Android ImageView点击选中后添加边框_第1张图片

 

 

你可能感兴趣的:(android)