Can anyone help me with best approach to the following? I have a SWT GUI java app developed using Eclipse. Not that all of that is particularly relevant.
I have a class to hold preferences that to simplify only has colors at moment. The option is a single integer held in a file that it successfully reads. The color_option is one of eight color schemes of 6 RGB type colors. So among others I tried:
class Preferences {
int color_option = 0;
String[] color_option_name = new String[8];
RGB[] rgb01=new RGB[8];
RGB[] rgb02=new RGB[8];
RGB[] rgb03=new RGB[8];
RGB[] rgb04=new RGB[8];
RGB[] rgb05=new RGB[8];
RGB[] rgb06=new RGB[8];
Color[] color01=new Color[8];
Color[] color02=new Color[8];
Color[] color03=new Color[8];
Color[] color04=new Color[8];
Color[] color05=new Color[8];
Color[] color06=new Color[8];
I have become aware that these colors are like literals and not variables so the above leads to a very bland display. And I tried:
if(color_option == 0) {
color_option_name[color_option] = "Earth";
rgb01[color_option]=new RGB(218, 165, 32);
color01[color_option]=new Color(Display.getCurrent(),rgb01[color_option]);
rgb02[color_option]=new RGB(188, 143, 143);
I want to use the colors for things like:
g0_opens_or_creates_project.setBackground(
and
if (script_deletion_flag[x]!=null && script_deletion_flag[x] ) {
itemS.setForeground(thisPreferences.color04[thisPreferences.color_option]);
}
else
{
itemS.setForeground(thisPreferences.color05[thisPreferences.color_option]);
}
Probably obvious but if anyone can shine a light on how to go about this without defining every color and lots of if statements this beginner would be very grateful. Tx in anticipation.
Well either no one uses color, it's difficult, or so simple no one could be bothered (could be lol) but for other beginners here is the best I have come up with and now implemented.
The problem revolves around RGB and Color not being variables and Color needing to be final.
So in my preferences file I have:
Earth Colors|218|165|32|188|143|143|178|34|34|107|142|35|143|188|143|189|183|107|
Pastel|127|255|212|255|182|193|238|233|233|32|178|170|186|85|211|210|180|140|
Navy|255|255|255|0|0|128|100|149|237|190|190|190|230|230|250|32|178|170|
Fire|233|150|122|250|128|114|255|160|122|255|165|0|255|140|0|255|127|80|
Gothic|128|0|128|0|0|0|255|255|255|192|192|192|221|160|221|255|228|196|
Bright Colors|255|20|147|0|255|0|255|255|0|0|255|255|255|0|0|0|128|128|
Bloom|238|221|130|205|92|92|255|140|0|255|69|0|255|0|0|232|150|122|
ICE|255|250|250|230|230|250|192|192|192|176|196|222|95|158|160|100|149|237|
So 8 color schemes of 6 colors each specified by an r, g and b integer (0-255).
And having read me preferences file coded like:
// This should be chosen color scheme
String[] color_rgb = lineoptions.split("\\|");
// Gothic
color_int[0] = Integer.valueOf(color_rgb[1]);
color_int[1] = Integer.valueOf(color_rgb[2]);
color_int[2] = Integer.valueOf(color_rgb[3]);
color_int[3] = Integer.valueOf(color_rgb[4]);
color_int[4] = Integer.valueOf(color_rgb[5]);
color_int[5] = Integer.valueOf(color_rgb[6]);
.....etc
Then:
RGB rgb101=new RGB(color_int[0], color_int[1], color_int[2]);
RGB rgb102=new RGB(color_int[3], color_int[4], color_int[5]);
RGB rgb103=new RGB(color_int[6], color_int[7], color_int[8]);
RGB rgb104=new RGB(color_int[9], color_int[10], color_int[11]);
RGB rgb105=new RGB(color_int[12], color_int[13], color_int[14]);
RGB rgb106=new RGB(color_int[15], color_int[16], color_int[17]);
final Color color01 = new Color(Display.getCurrent(), rgb101);
final Color color02 = new Color(Display.getCurrent(), rgb102);
final Color color03 = new Color(Display.getCurrent(), rgb103);
final Color color04 = new Color(Display.getCurrent(), rgb104);
final Color color05 = new Color(Display.getCurrent(), rgb105);
final Color color06 = new Color(Display.getCurrent(), rgb106);
Of course one has to restart the application before the new color selection takes effect.
Hope might help someone and possibly there are better answers? Cheers Nigel
Related
I have an IEnumerable of Color which I want to use as the basis for a brush.
At the moment, I convert the IEnumerable into a Bitmap, into a bitmapsource, into an imagebrush, but this is a bit slow, is there any brush class which can do what I want in a faster way?
edit, What I want to do: Use the brush in a pen to draw a line in a drawing visual, where the IEnumerable of Color is used as line color. If I have a collection of { Colours.Green, Colours.Red}, I want the resulting line to be half green, half red.
Here's a method that would convert your IEnumerable to a LinearGradientBrush.
There are 2 gradient stops for each color in order to create a hard transition between colours rather than a gradient.
LinearGradientBrush CreateBrush(IEnumerable<Color> colors) {
var colorArray = colors.ToArray();
var step = 1.0 / colorArray.Length;
var gradientStops = new GradientStopCollection();
for (int i = 0; i < colorArray.Length; i++) {
var color = colorArray[i];
gradientStops.Add(new GradientStop(color, i * step));
gradientStops.Add(new GradientStop(color, (i + 1) * step));
}
return new LinearGradientBrush(gradientStops);
}
I have the following png:
Each Icon is 100X100 px. All in all 800X100 px.
I have the following ImageView xml:
<ImageView
android:id="#+id/CycleStageImage"
android:layout_width="100dp"
android:layout_height="100dp">
I would like to set CycleStageImage to be show different Icon (100,100) on Timer Interval of 1 Second back and forth.
I am having a problem generating a code that moves on Axis of this PNG.
I have tried the followings from multiple links over SOF, but with no luck:
//first try - not working
//Resources res = mainActivity.ApplicationContext.Resources;
//Bitmap bitmap = BitmapFactory.DecodeResource(res, Resource.Id.CycleImage);
//BitmapDrawable bitmapDrawable = new BitmapDrawable(Resources.System, bitmap);
//ClipDrawable clipDrawable = new ClipDrawable(bitmapDrawable, GravityFlags.Center, ClipDrawable.Horizontal);
//clipDrawable.SetBounds(100, 100, 100, 100);
//clipDrawable.SetLevel(100);
//imageView.SetImageResource(Android.Resource.Color.Transparent);
//imageView.SetImageDrawable(clipDrawable);
//second try - shows only part of the left top corner
//double TUNNING = 0.5; //0.5 cut in half
//Bitmap srcBmp = BitmapFactory.DecodeResource(Resources.System, cycleStage);
//Bitmap modBmp = Bitmap.CreateBitmap(
// srcBmp,
// 0,
// srcBmp.Height, // TUNNING
// srcBmp.Height,
// srcBmp.Height
// );
//third try - same as the second try.
//int START_X = 0;
//int START_Y = 100;
//int WIDTH_PX = 100;
//int HEIGHT_PX = 100;
//// Crop bitmap
//Bitmap newBitmap = Bitmap.CreateBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);
//// Assign new bitmap to ImageView
//imageView.SetImageBitmap(newBitmap);
I have followed the Android tutorial:
https://developer.android.com/guide/topics/resources/drawable-resource.html#Clip
but with no luck..
It would be much appreciated the help with timer as well with the png.
Thanks!
Finally I got it to work, following these steps:
(Although I admit its a bit lame...)
I divided the above PNG into 8 (100x100) PNGs each using online tool -> PNG Splitter
I created a GIF using online tool - GIF Maker -> http://gifmaker.me/
I have placed the saved gif under "Assets" folder and changed its properties-> build action to "AndroidAsset"
I have created WebView and placed it inside my XML screen. it looks like this:
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyWebView"
android:layout_width="100dp"
android:layout_height="100dp"/>
In my Activity I generated the code as follows:
WebView myWebView = (WebView)findViewById(Resource.Id.MyWebView);
myWebView.loadUrl("file:///android_asset/Gif.gif");
It works like a charm!
I hope it will be of assistant to anyone who desires to accomplish running GIF's inside android application.
Good Luck.
I have set a Grid's background brush as an ImageBrush.
But when I set the Grid's FlowDirection to RightToLeft, the image is flipped horizontically.
Is it possible to (un)flip the grid background ImageBrush using a certain Transition or any other way?
Not much you can do about that with sensible means (there same means that are far from sensible).
Instead place an Image element as the first item in the Grid with Grid.RowSpan, Grid.ColumnSpan to cover all the cells. Use Stretch="Fill" on the Image since thats how a background typically behaves.
Well, i do understand that my comment is outdated, but this question is popping up one of the first in Google search, so here is my solution:
I was localizing the application for the right-to-left culture. The simple decision to set FlowDirection=RTL comes with unexpected drawbacks like the background containing the company logo is flipped. I have applied the matrix transformation for the image brush used to render the background:
var mbgBrush = TryFindResource("MainBackground") as Brush;
if (mbgBrush == null) return null;
if (FlowDirection == FlowDirection.LeftToRight) return mbgBrush;
var mainBgImageBrush = mbgBrush as ImageBrush;
if (mainBgImageBrush == null) return mbgBrush;
var flipXaxis = new MatrixTransform(-1.0, 0, 0, 1.0, 1, 0);
var flippedBrush = new ImageBrush
{
Stretch = Stretch.None,
Opacity = 1.0,
ImageSource = mainBgImageBrush.ImageSource,
RelativeTransform = flipXaxis
};
return flippedBrush;
I have this code that adds dotted lines under text in text box:
// Create an underline text decoration. Default is underline.
TextDecoration myUnderline = new TextDecoration();
// Create a linear gradient pen for the text decoration.
Pen myPen = new Pen();
myPen.Brush = new LinearGradientBrush(Colors.White, Colors.White, new Point(0, 0.5), new Point(1, 0.5));
myPen.Brush.Opacity = 0.5;
myPen.Thickness = 1.0;
myPen.DashStyle = DashStyles.Dash;
myUnderline.Pen = myPen;
myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to a TextDecorationCollection and add it to the text block.
TextDecorationCollection myCollection = new TextDecorationCollection();
myCollection.Add(myUnderline);
PasswordSendMessage.TextDecorations = myCollection;
My problem is I need only the last 6 characters in the text to be formatted!
Any idea how can I achieve that?
Instead of setting the property on the entire TextBlock, create a TextRange for the last six characters and apply the formatting to that:
var end = PasswordSendMessage.ContentEnd;
var start = end.GetPositionAtOffset(-6) ?? PasswordSendMessage.ContentStart;
var range = new TextRange(start, end);
range.ApplyPropertyValue(Inline.TextDecorationsProperty, myCollection);
If PasswordSendMessage is a TextBox rather than a TextBlock, then you cannot use rich text like this. You can use a RichTextBox, in which case this technique will work but you will need to use PasswordSendMessage.Document.ContentEnd and PasswordSendMessage.Document.ContentStart instead of PasswordSendMessage.ContentEnd and PasswordSendMessage.ContentStart.
You could databind your text to the Inlines property of TextBox and make a converter to build the run collection with a seperate Run for the last 6 characters applying your decorations
Are there any working piemenu controls for WPF?
I've found this in my favorite , you can take a look at :
This
have a nice day.
This question is probably long dead, but just a note that the control Thomas M posted, while awesome, has a major issue: You need to mouse over and click on the actual item instead of the pie slice. This means that the pie slices are not completely adjacent and IMO defeats a lot of the clickability (Frits's law) advantages of the control. So while it looks like a pie menu, it really just positions everything radially.
I ended up doing this:
private static Path makeDeliciousKeyLimePieSlice(double innerRadius, double outerRadius,
double startAngle, double endAngle, Vector ofs)
{
Point p1 = new Point(Math.Cos(endAngle) * innerRadius, Math.Sin(endAngle) * innerRadius) + ofs;
Point p2 = new Point(Math.Cos(startAngle) * innerRadius, Math.Sin(startAngle) * innerRadius) + ofs;
Point p3 = new Point(Math.Cos(startAngle) * outerRadius, Math.Sin(startAngle) * outerRadius) + ofs;
Point p4 = new Point(Math.Cos(endAngle) * outerRadius, Math.Sin(endAngle) * outerRadius) + ofs;
PathFigure fig = new PathFigure(p1, new PathSegment[] {
new ArcSegment(p2, new Size(innerRadius, innerRadius), endAngle - startAngle, false, SweepDirection.Counterclockwise, true),
new LineSegment(p3, true),
new ArcSegment(p4, new Size(outerRadius, outerRadius), startAngle - endAngle, false, SweepDirection.Clockwise, true),
}, true).GetAsFrozen();
return new Path { Data = new PathGeometry(new[] { fig }).GetAsFrozen() };
}
This will create a "slice" of the pie. You can style this how you want if you want a true pie menu. Another option is to make it transparent (set the fill to Brushes.Transparent; it must have a fill to be hit-test visible), which looks good for radial context menus. Here's my WIP after about half an hour's work (I know the spacing sucks):
alt text http://public.blu.livefilestore.com/y1pdW5ibqWquKGosMSch9C5KmOTKkiZ35mAI7iFKKUKf3cm7TGSquXhO8hkkL9Ln6Z3tKn74u67C27Qb_AIWQxzhg/radial.png?psid=1
EDIT: ah; the cursor doesn't appear in the shot -- basically, if you use the path overlay, you can have the mouse outside the actual control but still have it highlighted.
This control needs a bit of work still but it's a great starting point and supports multiple levels of items. (ie: a hierarchy) Check it out here