将网格视图添加到锁屏

时间:2017-10-06 06:39:12

标签: android lockscreen

我正在做一个带有gridview拼图的锁屏我已经成功地在屏幕和屏幕上触发了锁屏当我在我的lockcrean中只有按钮时,当我在我的锁屏中添加网格视图时,当我屏幕时,活动不会到来。< / p>

以下是我没有gridview的锁屏服务:

 public class LockscreenService extends Service {
private LinearLayout linearLayout;
private WindowManager.LayoutParams layoutParams;
private WindowManager windowManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
BroadcastReceiver screenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && linearLayout == null) {
            init();
        }
    }
};
@Override
public void onCreate() {
    super.onCreate();
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(screenReceiver, intentFilter);
    windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
    layoutParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION,
            PixelFormat.TRANSLUCENT);
}
private void init() {
    linearLayout = new LinearLayout(this);
    windowManager.addView(linearLayout, layoutParams);
    ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lockscreen, linearLayout);
 //   View btnClose = linearLayout.findViewById(R.id.button);
    View btnClose = linearLayout.findViewById(R.id.button8);
  btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when the corky3 is clicked
            windowManager.removeView(linearLayout);
            linearLayout = null;
        }
    });
}

它提供了一个锁定屏幕,如lockscreen without gridview

当我将锁屏添加到init()方法中的linearlayout时,就像这样

public class LockscreenService extends Service {
String p;
ImageView myImage;
final ArrayList<Bitmap> beforeshuffle = new ArrayList<>(9);
final ArrayList<Bitmap> aftershuffle = new ArrayList<>(9);
ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9);
GridView grid;
Bitmap bs;
Bitmap as;

private LinearLayout linearLayout;
private WindowManager.LayoutParams layoutParams;
private WindowManager windowManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
BroadcastReceiver screenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && intent.getAction().equals(Intent.ACTION_SCREEN_ON)&& linearLayout == null) {
            init();
        }
    }
};
@Override
public void onCreate() {
    Log.d("jhg","inservice");
    super.onCreate();
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(screenReceiver, intentFilter);
    windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
    layoutParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION,
            PixelFormat.TRANSLUCENT);
}
private void init() {
 //   Log.d("hdkjfh","in init");
    linearLayout = new LinearLayout(this);
    windowManager.addView(linearLayout, layoutParams);
    ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lockscreen, linearLayout);
    //   View btnClose = linearLayout.findViewById(R.id.button);
 //   grid = (GridView) linearLayout.findViewById(R.id.gridView);
    // grid = (GridView) findViewById(R.id.gridView);
    Globalvariable c = new Globalvariable();

    String p = c.givedata();
    Log.d("fg", p);
    bs = c.giveBitmap();
    // Bitmap bitmap = (Bitmap) i.getParcelableExtra("bitmap");

    //smallimage_Numbers is to tell how many smallimage_s the image should split

    int smallimage_Numbers = 9;

    //Getting the source image to split

    //  ImageView image = (ImageView) findViewById(R.id.source_image);
    // File imgFile = new  File("profile.jpg"

    Bitmap myBitmap = BitmapFactory.decodeFile(p);
    //Drawable d = new BitmapDrawable(getResources(), myBitmap);
    // myImage= (ImageView) linearLayout.findViewById(R.id.My);
    //  myImage = (ImageView) findViewById(R.id.My);
    // myImage.setImageBitmap(myBitmap);
    // myImage.setVisibility(View.INVISIBLE);
    splitImage(myBitmap, smallimage_Numbers);

    View btnClose = linearLayout.findViewById(R.id.button8);

    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when the corky3 is clicked
            Bitmap as = getBitmapFromView(grid);

            if (bs.sameAs(as)) {
                Toast.makeText(getApplicationContext(), "CORRECT", Toast.LENGTH_SHORT).show();
                windowManager.removeView(linearLayout);
                linearLayout = null;
            } else {
                Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT).show();
            }

        }
    });
}


    //invoking this method makes the actual splitting of the source image to given number of smallimage_s


    //Image.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));


    /**
     * Splits the source image and show them all into a grid in a new activity
     *
     * @param
     * @param smallimage_Numbers The target number of small image smallimage_s to be formed from the source image
     */

public void splitImage(Bitmap bit, int smallimage_Numbers) {


    //For the number of rows and columns of the grid to be displayed

    int rows, cols;


    //For height and width of the small image smallimage_s

    int smallimage_Height, smallimage_Width;


    //To store all the small image smallimage_s in bitmap format in this list

    smallimages = new ArrayList<Bitmap>(smallimage_Numbers);


    //Getting the scaled bitmap of the source image

    // BitmapDrawable mydrawable = (BitmapDrawable) image.getDrawable();

    Bitmap bitmap = bit;

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

    rows = cols = (int) Math.sqrt(smallimage_Numbers);

    smallimage_Height = bitmap.getHeight() / rows;

    smallimage_Width = bitmap.getWidth() / cols;


    //xCo and yCo are the pixel positions of the image smallimage_s

    int yCo = 0;

    for (int x = 0; x < rows; x++) {

        int xCo = 0;

        for (int y = 0; y < cols; y++) {

            smallimages.add(Bitmap.createBitmap(scaledBitmap, xCo, yCo, smallimage_Width, smallimage_Height));

            xCo += smallimage_Width;

        }

        yCo += smallimage_Height;

    }
    Array[] in = new Array[9];


    //Collections.shuffle(smallimages);
    grid.setAdapter(new ImageAdapter(this, smallimages));
    grid.setNumColumns((int) Math.sqrt(smallimages.size()));

    Collections.shuffle(smallimages);
    grid.setAdapter(new ImageAdapter(this, smallimages));
    grid.setNumColumns((int) Math.sqrt(smallimages.size()));
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        int counter = 0;
        int firstclick;

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            counter++;
            if (counter % 2 == 0) {
                firstclick = position;
                Bitmap data1 = smallimages.get(position);
            } else {
                Bitmap swapImage = smallimages.get(position);
                smallimages.set(position, smallimages.get(firstclick));
                smallimages.set(firstclick, swapImage);
                grid.invalidateViews();
            }
            for (int i = 0; i < smallimages.size(); i++) {
                aftershuffle.add(smallimages.get(i));
            }
        }

    });

    //activity to show these smallimage_s into a grid

}


//to compare two bitmaps in gridview

{
    @Override
    public void onClick (View v){
    Bitmap as = getBitmapFromView(grid);

    if (bs.sameAs(as)) {
        Toast.makeText(getApplicationContext(), "CORRECT", Toast.LENGTH_SHORT).show();
        windowManager.removeView(linearLayout);
        linearLayout = null;
    } else {
        Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT).show();
    }
}
});
 */
   public static Bitmap getBitmapFromView(View view){
    Bitmap bitmap    =Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas =new Canvas(bitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable!=null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);

    return bitmap;
}

}

当我点击屏幕打开和关闭时,它什么都不返回。帮我在锁屏中显示我的混乱网格视图。

1 个答案:

答案 0 :(得分:0)

我找不到引用GridView“网格”的地方。这两行是否有意评论?您也可以提供ImageAdapter的实现吗?