Skip to content

Commit

Permalink
Fixed small bugs related to the old verison and added a first non per…
Browse files Browse the repository at this point in the history
…formant rounded corner feature (it calls onDraw 4 times). Improvements needed before release.
  • Loading branch information
cesards committed Sep 5, 2018
1 parent 3fd1ffb commit b4c46eb
Show file tree
Hide file tree
Showing 33 changed files with 2,008 additions and 343 deletions.
6 changes: 5 additions & 1 deletion cropimageview-samples/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
android:label="@string/title_activity_simple_network_crop" />
<activity
android:name=".test.TestActivity"
android:label="@string/title_activity_test"></activity>
android:label="@string/title_activity_test" />
<activity
android:theme="@style/Theme.MaterialComponents.NoActionBar"
android:name=".rounded_corners.RoundedCornerImagesActivity"
android:label="@string/title_activity_rounded_corner_images"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.os.Bundle;

import com.cesards.samples.cropimageview.rounded_corners.RoundedCornerImagesActivity;
import com.cesards.samples.cropimageview.simple_crop.SimpleCropActivity;
import com.cesards.samples.cropimageview.simple_network_crop.SimpleNetworkCropActivity;
import com.cesards.samples.cropimageview.test.TestActivity;
Expand All @@ -24,6 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.test).setOnClickListener(view ->
startActivity(new Intent(this, TestActivity.class))
);
findViewById(R.id.rounded_corners).setOnClickListener(view ->
startActivity(new Intent(this, RoundedCornerImagesActivity.class))
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cesards.samples.cropimageview.rounded_corners;

import com.cesards.cropimageview.crop.CropType;

import androidx.annotation.DrawableRes;

final class Image {

@DrawableRes private final int drawableResource;
private final boolean roundedCorners;
@CropType private final int cropType;

Image(@DrawableRes int drawableResource, boolean roundedCorners, int cropType) {
this.drawableResource = drawableResource;
this.roundedCorners = roundedCorners;
this.cropType = cropType;
}

public int drawableResource() {
return drawableResource;
}

public boolean roundedCorners() {
return roundedCorners;
}

public int cropType() {
return cropType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.cesards.samples.cropimageview.rounded_corners;

import com.cesards.cropimageview.crop.CropType;
import com.cesards.samples.cropimageview.R;

import java.util.Arrays;
import java.util.List;

final class ImageFactory {

private ImageFactory() {
throw new AssertionError("This shouldn't be initialized!");
}

static List<Image> imagesWithoutRoundedCorners() {
return Arrays.asList(
// new Image(R.drawable.ball_vertical, false, CropType.NONE),
// new Image(R.drawable.ball_horizontal, false, CropType.NONE),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER)
);
}

static List<Image> imagesWithRoundedCorners() {
return Arrays.asList(
// new Image(R.drawable.ball_vertical, false, CropType.NONE),
// new Image(R.drawable.ball_horizontal, false, CropType.NONE),
new Image(R.drawable.ball_vertical, true, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, true, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP),
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER),
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM),
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.cesards.samples.cropimageview.rounded_corners;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cesards.cropimageview.CropImageView;
import com.cesards.samples.cropimageview.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

final class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ItemImageViewHolder> {

private final List<Image> images = new ArrayList<>();

void removeAll() {
if (images.isEmpty()) {
return;
}
int imagesLength = images.size();
images.clear();
notifyItemRangeRemoved(0, imagesLength);
}

void add(Image image) {
images.add(image);
notifyItemInserted(this.images.size() - 1);
}

void add(List<Image> images) {
this.images.addAll(images);
notifyItemRangeInserted(this.images.size() - 1, images.size());
}

@NonNull
@Override
public ItemImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ItemImageViewHolder(LayoutInflater.from(
parent.getContext()).inflate(R.layout.item_image, parent, false)
);
}

@Override
public void onBindViewHolder(@NonNull ItemImageViewHolder itemImageViewHolder, int position) {
itemImageViewHolder.bind(images.get(position));
}

@Override
public int getItemCount() {
return images.size();
}

static class ItemImageViewHolder extends RecyclerView.ViewHolder {

private final CropImageView cropImageView;

ItemImageViewHolder(@NonNull View itemView) {
super(itemView);
this.cropImageView = (CropImageView) itemView;
}

void bind(Image image) {
cropImageView.setImageDrawable(ContextCompat.getDrawable(itemView.getContext(), image.drawableResource()));
cropImageView.croppedImage().withCropType(image.cropType());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.cesards.samples.cropimageview.rounded_corners;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.cesards.samples.cropimageview.R;
import com.cesards.samples.cropimageview.rounded_corners.widget.VerticalTransparentItemDecorator;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

public class RoundedCornerImagesActivity extends AppCompatActivity {

private ImagesAdapter imagesAdapter = new ImagesAdapter();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rounded_corner_images);
setupViews(savedInstanceState);
}

private void setupViews(Bundle savedInstanceState) {
setSupportActionBar(findViewById(R.id.title_container));
// getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);

RecyclerView images = findViewById(R.id.images);
images.addItemDecoration(new VerticalTransparentItemDecorator(getResources().getDimensionPixelOffset(R.dimen.activity_horizontal_margin)));
images.setAdapter(imagesAdapter);

Spinner navSpinner = findViewById(R.id.image_options);

navSpinner.setAdapter(
ArrayAdapter.createFromResource(
navSpinner.getContext(),
R.array.options,
android.R.layout.simple_spinner_dropdown_item
)
);

navSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
imagesAdapter.removeAll();
imagesAdapter.add(ImageFactory.imagesWithoutRoundedCorners());
break;

case 1:
imagesAdapter.removeAll();
imagesAdapter.add(ImageFactory.imagesWithRoundedCorners());
break;

default:
throw new IllegalStateException("If it exists you can handle it.");
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) { }
});

if (savedInstanceState == null) {
navSpinner.setSelection(0);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cesards.samples.cropimageview.rounded_corners.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class VerticalTransparentItemDecorator extends RecyclerView.ItemDecoration {

private int space;

public VerticalTransparentItemDecorator(int value) {
this.space = value;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// Skip first item in the list.
if (parent.getChildAdapterPosition(view) != 0) {
outRect.set(0, space, 0, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.view.LayoutInflater;
import android.widget.ImageView;

import com.cesards.cropimageview.model.CropType;
import com.cesards.cropimageview.crop.CropType;
import com.cesards.samples.cropimageview.R;
import com.cesards.samples.cropimageview._activity.CommonImagesAdapter;
import com.cesards.samples.cropimageview._util.SystemUiHelper;
Expand Down Expand Up @@ -54,7 +54,7 @@ public ImageView instantiatePagerItem(int position) {
// cropImageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ball_horizontal));
cropImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else {
// ((CropImageView) cropImageView).setCropType(images[position - 1]);
// ((CropImageView) cropImageView).withCropType(images[position - 1]);
}
return cropImageView;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.cesards.samples.cropimageview.simple_network_crop;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.widget.ImageView;

import com.cesards.cropimageview.CropImageView;
import com.cesards.cropimageview.model.CropType;
import com.cesards.cropimageview.crop.CropType;
import com.cesards.samples.cropimageview.R;
import com.cesards.samples.cropimageview._activity.CommonImagesAdapter;
import com.cesards.samples.cropimageview._util.SystemUiHelper;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

public class SimpleNetworkCropActivity extends AppCompatActivity implements CommonImagesAdapter {
Expand Down Expand Up @@ -55,21 +54,13 @@ public ImageView instantiatePagerItem(int position) {
// cropImageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ball_horizontal));
if (position == 0) {

// cropImageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ball_horizontal));
cropImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// cropImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
cropImageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ball_horizontal));
} else {
Picasso.get().load(R.drawable.ball_horizontal).into(cropImageView, new Callback() {
@Override
public void onSuccess() {
cropImageView.setCropType(CropType.LEFT_CENTER);
}

@Override
public void onError(Exception e) {

}
});
// ((CropImageView) cropImageView).setCropType(images[position - 1]);
cropImageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ball_horizontal));
// cropImageView.croppedImage().withCropType(CropType.LEFT_CENTER);
// Picasso.get().load(R.drawable.ball_horizontal).into(cropImageView);
// ((CropImageView) cropImageView).withCropType(images[position - 1]);
}
return cropImageView;
}
Expand Down
Loading

0 comments on commit b4c46eb

Please sign in to comment.